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