]> git.openstreetmap.org Git - rails.git/blob - public/openlayers/OpenStreetMap.js
40722d4e0bc81c359a3d638f554dc00d3164ee4c
[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         var newArguments = [name, url, options];
113         OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
114     },    
115
116     CLASS_NAME: "OpenLayers.Layer.OSM.Mapnik"
117 });
118
119 /**
120  * Class: OpenLayers.Layer.OSM.Osmarender
121  * 
122  * Inherits from:
123  *  - <OpenLayers.Layer.OSM>
124  */
125 OpenLayers.Layer.OSM.Osmarender = OpenLayers.Class(OpenLayers.Layer.OSM, {
126     /**
127      * Constructor: OpenLayers.Layer.OSM.Osmarender
128      * 
129      * Parameters:
130      * name - {String}
131      * options - {Object} Hashtable of extra options to tag onto the layer
132      */
133     initialize: function(name, options) {
134         var url = [
135             "http://a.tah.openstreetmap.org/Tiles/tile.php/",
136             "http://b.tah.openstreetmap.org/Tiles/tile.php/",
137             "http://c.tah.openstreetmap.org/Tiles/tile.php/"
138         ];
139         var newArguments = [name, url, options];
140         OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
141     },    
142
143     CLASS_NAME: "OpenLayers.Layer.OSM.Osmarender"
144 });