]> git.openstreetmap.org Git - rails.git/blob - public/openlayers/OpenStreetMap.js
Change postcode searches to use z15 instead of z12.
[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 {
25         OpenLayers.Util.OSM.originalOnImageLoadError;
26     }
27 };
28
29 /**
30  * @requires OpenLayers/Layer/TMS.js
31  *
32  * Class: OpenLayers.Layer.OSM
33  *
34  * Inherits from:
35  *  - <OpenLayers.Layer.TMS>
36  */
37 OpenLayers.Layer.OSM = OpenLayers.Class(OpenLayers.Layer.TMS, {
38     /**
39      * Constructor: OpenLayers.Layer.OSM
40      *
41      * Parameters:
42      * name - {String}
43      * url - {String}
44      * options - {Object} Hashtable of extra options to tag onto the layer
45      */
46     initialize: function(name, url, options) {
47         options = OpenLayers.Util.extend(options, {
48             attribution: "Data by <a href='http://openstreetmap.org/'>OpenStreetMap</a>",
49             maxExtent: new OpenLayers.Bounds(-20037508,-20037508,20037508,20037508),
50             maxResolution: 156543,
51             units: "m",
52             projection: "EPSG:41001"
53         });
54         OpenLayers.Layer.TMS.prototype.initialize.apply(this, arguments);
55     },
56
57     /**
58      * Method: getUrl
59      *
60      * Parameters:
61      * bounds - {<OpenLayers.Bounds>}
62      *
63      * Returns:
64      * {String} A string with the layer's url and parameters and also the
65      *          passed-in bounds and appropriate tile size specified as
66      *          parameters
67      */
68     getURL: function (bounds) {
69         var res = this.map.getResolution();
70         var x = Math.round((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
71         var y = Math.round((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
72         var z = this.map.getZoom();
73         var limit = Math.pow(2, z);
74
75         if (y < 0 || y >= limit)
76         {
77             return OpenLayers.Util.OSM.MISSING_TILE_URL;
78         }
79         else
80         {
81             x = ((x % limit) + limit) % limit;
82
83             var url = this.url;
84             var path = z + "/" + x + "/" + y + ".png";
85
86             if (url instanceof Array)
87             {
88                 url = this.selectUrl(path, url);
89             }
90
91             return url + path;
92         }
93     },
94
95     CLASS_NAME: "OpenLayers.Layer.OSM"
96 });
97
98 /**
99  * Class: OpenLayers.Layer.OSM.Mapnik
100  *
101  * Inherits from:
102  *  - <OpenLayers.Layer.OSM>
103  */
104 OpenLayers.Layer.OSM.Mapnik = OpenLayers.Class(OpenLayers.Layer.OSM, {
105     /**
106      * Constructor: OpenLayers.Layer.OSM.Mapnik
107      *
108      * Parameters:
109      * name - {String}
110      * options - {Object} Hashtable of extra options to tag onto the layer
111      */
112     initialize: function(name, options) {
113         var url = [
114             "http://a.tile.openstreetmap.org/",
115             "http://b.tile.openstreetmap.org/",
116             "http://c.tile.openstreetmap.org/"
117         ];
118         options = OpenLayers.Util.extend(options, { numZoomLevels: 19 });
119         var newArguments = [name, url, options];
120         OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
121     },
122
123     CLASS_NAME: "OpenLayers.Layer.OSM.Mapnik"
124 });
125
126 /**
127  * Class: OpenLayers.Layer.OSM.Osmarender
128  *
129  * Inherits from:
130  *  - <OpenLayers.Layer.OSM>
131  */
132 OpenLayers.Layer.OSM.Osmarender = OpenLayers.Class(OpenLayers.Layer.OSM, {
133     /**
134      * Constructor: OpenLayers.Layer.OSM.Osmarender
135      *
136      * Parameters:
137      * name - {String}
138      * options - {Object} Hashtable of extra options to tag onto the layer
139      */
140     initialize: function(name, options) {
141         var url = [
142             "http://a.tah.openstreetmap.org/Tiles/tile.php/",
143             "http://b.tah.openstreetmap.org/Tiles/tile.php/",
144             "http://c.tah.openstreetmap.org/Tiles/tile.php/"
145         ];
146         options = OpenLayers.Util.extend(options, { numZoomLevels: 18 });
147         var newArguments = [name, url, options];
148         OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
149     },
150
151     CLASS_NAME: "OpenLayers.Layer.OSM.Osmarender"
152 });