]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/leaflet.map.js
Add public gps tiles layer for debugging purposes
[rails.git] / app / assets / javascripts / leaflet.map.js
1 L.extend(L.LatLngBounds.prototype, {
2   getSize: function () {
3     return (this._northEast.lat - this._southWest.lat) *
4            (this._northEast.lng - this._southWest.lng);
5   },
6
7   wrap: function () {
8     return new L.LatLngBounds(this._southWest.wrap(), this._northEast.wrap());
9   }
10 });
11
12 L.OSM.Map = L.Map.extend({
13   initialize: function(id, options) {
14     L.Map.prototype.initialize.call(this, id, options);
15
16     var copyright = I18n.t('javascripts.map.copyright', {copyright_url: '/copyright'});
17     var donate = I18n.t('javascripts.map.donate_link_text', {donate_url: 'http://donate.openstreetmap.org'});
18
19     this.baseLayers = [];
20
21     this.baseLayers.push(new L.OSM.Mapnik({
22       attribution: copyright + " ♥ " + donate,
23       code: "M",
24       keyid: "mapnik",
25       name: I18n.t("javascripts.map.base.standard")
26     }));
27
28     if (OSM.THUNDERFOREST_KEY) {
29       this.baseLayers.push(new L.OSM.CycleMap({
30         attribution: copyright + ". Tiles courtesy of <a href='http://www.thunderforest.com/' target='_blank'>Andy Allan</a>",
31         apikey: OSM.THUNDERFOREST_KEY,
32         code: "C",
33         keyid: "cyclemap",
34         name: I18n.t("javascripts.map.base.cycle_map")
35       }));
36
37       this.baseLayers.push(new L.OSM.TransportMap({
38         attribution: copyright + ". Tiles courtesy of <a href='http://www.thunderforest.com/' target='_blank'>Andy Allan</a>",
39         apikey: OSM.THUNDERFOREST_KEY,
40         code: "T",
41         keyid: "transportmap",
42         name: I18n.t("javascripts.map.base.transport_map")
43       }));
44     }
45
46     this.baseLayers.push(new L.OSM.HOT({
47       attribution: copyright + ". Tiles courtesy of <a href='http://hot.openstreetmap.org/' target='_blank'>Humanitarian OpenStreetMap Team</a>",
48       code: "H",
49       keyid: "hot",
50       name: I18n.t("javascripts.map.base.hot")
51     }));
52
53     this.noteLayer = new L.FeatureGroup();
54     this.noteLayer.options = {code: 'N'};
55
56     this.dataLayer = new L.OSM.DataLayer(null);
57     this.dataLayer.options.code = 'D';
58
59     this.gpsLayer = new L.OSM.GPS({
60       code: "G",
61       name: I18n.t("javascripts.map.base.gps")
62     });
63   },
64
65   updateLayers: function(layerParam) {
66     layerParam = layerParam || "M";
67     var layersAdded = "";
68
69     for (var i = this.baseLayers.length - 1; i >= 0; i--) {
70       if (layerParam.indexOf(this.baseLayers[i].options.code) >= 0) {
71         this.addLayer(this.baseLayers[i]);
72         layersAdded = layersAdded + this.baseLayers[i].options.code;
73       } else if (i === 0 && layersAdded === "") {
74         this.addLayer(this.baseLayers[i]);
75       } else {
76         this.removeLayer(this.baseLayers[i]);
77       }
78     }
79   },
80
81   getLayersCode: function () {
82     var layerConfig = '';
83     for (var i in this._layers) { // TODO: map.eachLayer
84       var layer = this._layers[i];
85       if (layer.options && layer.options.code) {
86         layerConfig += layer.options.code;
87       }
88     }
89     return layerConfig;
90   },
91
92   getMapBaseLayerId: function () {
93     for (var i in this._layers) { // TODO: map.eachLayer
94       var layer = this._layers[i];
95       if (layer.options && layer.options.keyid) return layer.options.keyid;
96     }
97   },
98
99   getUrl: function(marker) {
100     var precision = OSM.zoomPrecision(this.getZoom()),
101         params = {};
102
103     if (marker && this.hasLayer(marker)) {
104       var latLng = marker.getLatLng().wrap();
105       params.mlat = latLng.lat.toFixed(precision);
106       params.mlon = latLng.lng.toFixed(precision);
107     }
108
109     var url = 'http://' + OSM.SERVER_URL + '/',
110       query = querystring.stringify(params),
111       hash = OSM.formatHash(this);
112
113     if (query) url += '?' + query;
114     if (hash) url += hash;
115
116     return url;
117   },
118
119   getShortUrl: function(marker) {
120     var zoom = this.getZoom(),
121       latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
122       str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
123         'http://osm.org/go/' : 'http://' + window.location.hostname + '/go/',
124       char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
125       x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
126       y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
127       // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
128       // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
129       // and drops the last 4 bits of the full 64 bit Morton code.
130       c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
131       digit;
132
133     for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
134       digit = (c1 >> (24 - 6 * i)) & 0x3f;
135       str += char_array.charAt(digit);
136     }
137     for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
138       digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
139       str += char_array.charAt(digit);
140     }
141     for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
142
143     // Called to interlace the bits in x and y, making a Morton code.
144     function interlace(x, y) {
145       x = (x | (x << 8)) & 0x00ff00ff;
146       x = (x | (x << 4)) & 0x0f0f0f0f;
147       x = (x | (x << 2)) & 0x33333333;
148       x = (x | (x << 1)) & 0x55555555;
149       y = (y | (y << 8)) & 0x00ff00ff;
150       y = (y | (y << 4)) & 0x0f0f0f0f;
151       y = (y | (y << 2)) & 0x33333333;
152       y = (y | (y << 1)) & 0x55555555;
153       return (x << 1) | y;
154     }
155
156     var params = {};
157     var layers = this.getLayersCode().replace('M', '');
158
159     if (layers) {
160       params.layers = layers;
161     }
162
163     if (marker && this.hasLayer(marker)) {
164       params.m = '';
165     }
166
167     if (this._object) {
168       params[this._object.type] = this._object.id;
169     }
170
171     var query = querystring.stringify(params);
172     if (query) {
173       str += '?' + query;
174     }
175
176     return str;
177   },
178
179   getGeoUri: function(marker) {
180     var precision = OSM.zoomPrecision(this.getZoom()),
181         latLng,
182         params = {};
183
184     if (marker && this.hasLayer(marker)) {
185       latLng = marker.getLatLng().wrap();
186     } else {
187       latLng = this.getCenter();
188     }
189
190     params.lat = latLng.lat.toFixed(precision);
191     params.lon = latLng.lng.toFixed(precision);
192     params.zoom = this.getZoom();
193
194     return 'geo:' + params.lat + ',' + params.lon + '?z=' + params.zoom;
195   },
196
197   addObject: function(object, callback) {
198     var objectStyle = {
199       color: "#FF6200",
200       weight: 4,
201       opacity: 1,
202       fillOpacity: 0.5
203     };
204
205     var changesetStyle = {
206       weight: 4,
207       color: '#FF9500',
208       opacity: 1,
209       fillOpacity: 0,
210       clickable: false
211     };
212
213     this._object = object;
214
215     if (this._objectLoader) this._objectLoader.abort();
216     if (this._objectLayer) this.removeLayer(this._objectLayer);
217
218     var map = this;
219     this._objectLoader = $.ajax({
220       url: OSM.apiUrl(object),
221       dataType: "xml",
222       success: function (xml) {
223         map._objectLayer = new L.OSM.DataLayer(null, {
224           styles: {
225             node: objectStyle,
226             way: objectStyle,
227             area: objectStyle,
228             changeset: changesetStyle
229           }
230         });
231
232         map._objectLayer.interestingNode = function (node, ways, relations) {
233           if (object.type === "node") {
234             return true;
235           } else if (object.type === "relation") {
236             for (var i = 0; i < relations.length; i++)
237               if (relations[i].members.indexOf(node) !== -1)
238                 return true;
239           } else {
240             return false;
241           }
242         };
243
244         map._objectLayer.addData(xml);
245         map._objectLayer.addTo(map);
246
247         if (callback) callback(map._objectLayer.getBounds());
248       }
249     });
250   },
251
252   removeObject: function() {
253     this._object = null;
254     if (this._objectLoader) this._objectLoader.abort();
255     if (this._objectLayer) this.removeLayer(this._objectLayer);
256   },
257
258   getState: function() {
259     return {
260       center: this.getCenter().wrap(),
261       zoom: this.getZoom(),
262       layers: this.getLayersCode()
263     };
264   },
265
266   setState: function(state, options) {
267     if (state.center) this.setView(state.center, state.zoom, options);
268     if (state.layers) this.updateLayers(state.layers);
269   },
270
271   setSidebarOverlaid: function(overlaid) {
272     if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
273       $("#content").addClass("overlay-sidebar");
274       this.invalidateSize({pan: false})
275         .panBy([-350, 0], {animate: false});
276     } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
277       this.panBy([350, 0], {animate: false});
278       $("#content").removeClass("overlay-sidebar");
279       this.invalidateSize({pan: false});
280     }
281     return this;
282   }
283 });
284
285 L.Icon.Default.imagePath = "/images/";
286
287 L.Icon.Default.imageUrls = {
288   "/images/marker-icon.png": OSM.MARKER_ICON,
289   "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
290   "/images/marker-shadow.png": OSM.MARKER_SHADOW
291 };
292
293 L.extend(L.Icon.Default.prototype, {
294   _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
295
296   _getIconUrl:  function (name) {
297     var url = this._oldGetIconUrl(name);
298     return L.Icon.Default.imageUrls[url];
299   }
300 });
301
302 OSM.getUserIcon = function (url) {
303   return L.icon({
304     iconUrl: url || OSM.MARKER_RED,
305     iconSize: [25, 41],
306     iconAnchor: [12, 41],
307     popupAnchor: [1, -34],
308     shadowUrl: OSM.MARKER_SHADOW,
309     shadowSize: [41, 41]
310   });
311 };