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