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