]> git.openstreetmap.org Git - rails.git/blob - public/openlayers/OpenStreetMap.js
Merge 15722:15806 from head and adjust new views to rails 2 style names.
[rails.git] / public / openlayers / OpenStreetMap.js
1 /**
2  * Namespace: Util.OSM
3  */
4 OpenLayers.Util.OSM = {};
5
6 /**
7  * Constant: MISSING_TILE_URL
8  * {String} URL of image to display for missing tiles
9  */
10 OpenLayers.Util.OSM.MISSING_TILE_URL = "http://openstreetmap.org/openlayers/img/404.png";
11
12 /**
13  * Property: originalOnImageLoadError
14  * {Function} Original onImageLoadError function.
15  */
16 OpenLayers.Util.OSM.originalOnImageLoadError = OpenLayers.Util.onImageLoadError;
17
18 /**
19  * Function: onImageLoadError
20  */
21 OpenLayers.Util.onImageLoadError = function() {
22     if (this.src.match(/^http:\/\/[abc]\.[a-z]+\.openstreetmap\.org\//)) {
23         this.src = OpenLayers.Util.OSM.MISSING_TILE_URL;
24     } else if (this.src.match(/^http:\/\/[def]\.tah\.openstreetmap\.org\//)) {
25         // do nothing - this layer is transparent
26     } else {
27         OpenLayers.Util.OSM.originalOnImageLoadError;
28     }
29 };
30
31 /**
32  * @requires OpenLayers/Layer/TMS.js
33  *
34  * Class: OpenLayers.Layer.OSM
35  *
36  * Inherits from:
37  *  - <OpenLayers.Layer.TMS>
38  */
39 OpenLayers.Layer.OSM = OpenLayers.Class(OpenLayers.Layer.TMS, {
40     /**
41      * Constructor: OpenLayers.Layer.OSM
42      *
43      * Parameters:
44      * name - {String}
45      * url - {String}
46      * options - {Object} Hashtable of extra options to tag onto the layer
47      */
48     initialize: function(name, url, options) {
49         options = OpenLayers.Util.extend({
50             attribution: "Data by <a href='http://openstreetmap.org/'>OpenStreetMap</a> under <a href='http://creativecommons.org/licenses/by-sa/2.0/'>CC-BY-SA</a>",
51             maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
52             maxResolution: 156543.0339,
53             units: "m",
54             projection: "EPSG:900913",
55             transitionEffect: "resize"
56         }, options);
57         var newArguments = [name, url, options];
58         OpenLayers.Layer.TMS.prototype.initialize.apply(this, newArguments);
59     },
60
61     /**
62      * Method: getUrl
63      *
64      * Parameters:
65      * bounds - {<OpenLayers.Bounds>}
66      *
67      * Returns:
68      * {String} A string with the layer's url and parameters and also the
69      *          passed-in bounds and appropriate tile size specified as
70      *          parameters
71      */
72     getURL: function (bounds) {
73         var res = this.map.getResolution();
74         var x = Math.round((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
75         var y = Math.round((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
76         var z = this.map.getZoom();
77         var limit = Math.pow(2, z);
78
79         if (y < 0 || y >= limit)
80         {
81             return OpenLayers.Util.OSM.MISSING_TILE_URL;
82         }
83         else
84         {
85             x = ((x % limit) + limit) % limit;
86
87             var url = this.url;
88             var path = z + "/" + x + "/" + y + ".png";
89
90             if (url instanceof Array)
91             {
92                 url = this.selectUrl(path, url);
93             }
94
95             return url + path;
96         }
97     },
98
99     CLASS_NAME: "OpenLayers.Layer.OSM"
100 });
101
102 /**
103  * Class: OpenLayers.Layer.OSM.Mapnik
104  *
105  * Inherits from:
106  *  - <OpenLayers.Layer.OSM>
107  */
108 OpenLayers.Layer.OSM.Mapnik = OpenLayers.Class(OpenLayers.Layer.OSM, {
109     /**
110      * Constructor: OpenLayers.Layer.OSM.Mapnik
111      *
112      * Parameters:
113      * name - {String}
114      * options - {Object} Hashtable of extra options to tag onto the layer
115      */
116     initialize: function(name, options) {
117         var url = [
118             "http://a.tile.openstreetmap.org/",
119             "http://b.tile.openstreetmap.org/",
120             "http://c.tile.openstreetmap.org/"
121         ];
122         options = OpenLayers.Util.extend({ numZoomLevels: 19 }, options);
123         var newArguments = [name, url, options];
124         OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
125     },
126
127     CLASS_NAME: "OpenLayers.Layer.OSM.Mapnik"
128 });
129
130 /**
131  * Class: OpenLayers.Layer.OSM.Osmarender
132  *
133  * Inherits from:
134  *  - <OpenLayers.Layer.OSM>
135  */
136 OpenLayers.Layer.OSM.Osmarender = OpenLayers.Class(OpenLayers.Layer.OSM, {
137     /**
138      * Constructor: OpenLayers.Layer.OSM.Osmarender
139      *
140      * Parameters:
141      * name - {String}
142      * options - {Object} Hashtable of extra options to tag onto the layer
143      */
144     initialize: function(name, options) {
145         var url = [
146             "http://a.tah.openstreetmap.org/Tiles/tile/",
147             "http://b.tah.openstreetmap.org/Tiles/tile/",
148             "http://c.tah.openstreetmap.org/Tiles/tile/"
149         ];
150         options = OpenLayers.Util.extend({ numZoomLevels: 18 }, options);
151         var newArguments = [name, url, options];
152         OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
153     },
154
155     CLASS_NAME: "OpenLayers.Layer.OSM.Osmarender"
156 });
157
158 /**
159  * Class: OpenLayers.Layer.OSM.CycleMap
160  *
161  * Inherits from:
162  *  - <OpenLayers.Layer.OSM>
163  */
164 OpenLayers.Layer.OSM.CycleMap = OpenLayers.Class(OpenLayers.Layer.OSM, {
165     /**
166      * Constructor: OpenLayers.Layer.OSM.CycleMap
167      *
168      * Parameters:
169      * name - {String}
170      * options - {Object} Hashtable of extra options to tag onto the layer
171      */
172     initialize: function(name, options) {
173         var url = [
174             "http://a.andy.sandbox.cloudmade.com/tiles/cycle/",
175             "http://b.andy.sandbox.cloudmade.com/tiles/cycle/",
176             "http://c.andy.sandbox.cloudmade.com/tiles/cycle/"
177         ];
178         options = OpenLayers.Util.extend({ numZoomLevels: 19 }, options);
179         var newArguments = [name, url, options];
180         OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
181     },
182
183     CLASS_NAME: "OpenLayers.Layer.OSM.CycleMap"
184 });
185
186 /**
187  * Class: OpenLayers.Layer.OSM.Maplint
188  *
189  * Inherits from:
190  *  - <OpenLayers.Layer.OSM>
191  */
192 OpenLayers.Layer.OSM.Maplint = OpenLayers.Class(OpenLayers.Layer.OSM, {
193     /**
194      * Constructor: OpenLayers.Layer.OSM.Maplint
195      *
196      * Parameters:
197      * name - {String}
198      * options - {Object} Hashtable of extra options to tag onto the layer
199      */
200     initialize: function(name, options) {
201         var url = [
202             "http://d.tah.openstreetmap.org/Tiles/maplint/",
203             "http://e.tah.openstreetmap.org/Tiles/maplint/",
204             "http://f.tah.openstreetmap.org/Tiles/maplint/"
205         ];
206         options = OpenLayers.Util.extend({ numZoomLevels: 18, isBaseLayer: false, visibility: false }, options);
207         var newArguments = [name, url, options];
208         OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
209     },
210
211     CLASS_NAME: "OpenLayers.Layer.OSM.Maplint"
212 });