3 L.extend(L.LatLngBounds.prototype, {
5 return (this._northEast.lat - this._southWest.lat) *
6 (this._northEast.lng - this._southWest.lng);
10 return new L.LatLngBounds(this._southWest.wrap(), this._northEast.wrap());
14 L.OSM.Map = L.Map.extend({
15 initialize: function (id, options) {
16 L.Map.prototype.initialize.call(this, id, options);
18 const layerDefinitions = [
20 leafletOsmId: "Mapnik",
25 id: "make_a_donation",
26 href: "https://supporting.openstreetmap.org",
31 leafletOsmId: "CyclOSM",
40 href: "https://www.cyclosm.org"
44 href: "https://openstreetmap.fr/"
50 leafletOsmId: "CycleMap",
54 apiKeyId: "THUNDERFOREST_KEY",
56 id: "thunderforest_credit",
60 href: "https://www.thunderforest.com/"
66 leafletOsmId: "TransportMap",
68 keyId: "transportmap",
69 nameId: "transport_map",
70 apiKeyId: "THUNDERFOREST_KEY",
72 id: "thunderforest_credit",
76 href: "https://www.thunderforest.com/"
82 leafletOsmId: "TracestrackTopo",
84 keyId: "tracestracktopo",
85 nameId: "tracestracktop_topo",
86 apiKeyId: "TRACESTRACK_KEY",
88 id: "tracestrack_credit",
92 href: "https://www.tracestrack.com/"
107 href: "https://www.hotosm.org/"
111 href: "https://openstreetmap.fr/"
118 this.baseLayers = [];
120 for (const layerDefinition of layerDefinitions) {
121 if (layerDefinition.apiKeyId && !OSM[layerDefinition.apiKeyId]) continue;
123 const layerOptions = {
124 attribution: makeAttribution(layerDefinition.credit),
125 code: layerDefinition.code,
126 keyid: layerDefinition.keyId,
127 name: I18n.t(`javascripts.map.base.${layerDefinition.nameId}`)
129 if (layerDefinition.apiKeyId) {
130 layerOptions.apikey = OSM[layerDefinition.apiKeyId];
133 const layer = new L.OSM[layerDefinition.leafletOsmId](layerOptions);
134 this.baseLayers.push(layer);
137 this.noteLayer = new L.FeatureGroup();
138 this.noteLayer.options = { code: "N" };
140 this.dataLayer = new L.OSM.DataLayer(null);
141 this.dataLayer.options.code = "D";
143 this.gpsLayer = new L.OSM.GPS({
148 this.on("layeradd", function (event) {
149 if (this.baseLayers.indexOf(event.layer) >= 0) {
150 this.setMaxZoom(event.layer.options.maxZoom);
154 function makeAttribution(credit) {
155 let attribution = "";
157 attribution += I18n.t("javascripts.map.copyright_text", {
158 copyright_link: $("<a>", {
160 text: I18n.t("javascripts.map.openstreetmap_contributors")
164 attribution += credit.donate ? " ♥ " : ". ";
165 attribution += makeCredit(credit);
168 attribution += $("<a>", {
169 href: "https://wiki.osmfoundation.org/wiki/Terms_of_Use",
170 text: I18n.t("javascripts.map.website_and_api_terms")
171 }).prop("outerHTML");
176 function makeCredit(credit) {
178 for (const childId in credit.children) {
179 children[childId] = makeCredit(credit.children[childId]);
181 const text = I18n.t(`javascripts.map.${credit.id}`, children);
183 const link = $("<a>", {
188 link.addClass("donate-attr");
190 link.attr("target", "_blank");
192 return link.prop("outerHTML");
199 updateLayers: function (layerParam) {
200 var layers = layerParam || "M",
203 for (var i = this.baseLayers.length - 1; i >= 0; i--) {
204 if (layers.indexOf(this.baseLayers[i].options.code) >= 0) {
205 this.addLayer(this.baseLayers[i]);
206 layersAdded = layersAdded + this.baseLayers[i].options.code;
207 } else if (i === 0 && layersAdded === "") {
208 this.addLayer(this.baseLayers[i]);
210 this.removeLayer(this.baseLayers[i]);
215 getLayersCode: function () {
216 var layerConfig = "";
217 this.eachLayer(function (layer) {
218 if (layer.options && layer.options.code) {
219 layerConfig += layer.options.code;
225 getMapBaseLayerId: function () {
227 this.eachLayer(function (layer) {
228 if (layer.options && layer.options.keyid) baseLayerId = layer.options.keyid;
233 getUrl: function (marker) {
234 var precision = OSM.zoomPrecision(this.getZoom()),
237 if (marker && this.hasLayer(marker)) {
238 var latLng = marker.getLatLng().wrap();
239 params.mlat = latLng.lat.toFixed(precision);
240 params.mlon = latLng.lng.toFixed(precision);
243 var url = window.location.protocol + "//" + OSM.SERVER_URL + "/",
244 query = Qs.stringify(params),
245 hash = OSM.formatHash(this);
247 if (query) url += "?" + query;
248 if (hash) url += hash;
253 getShortUrl: function (marker) {
254 var zoom = this.getZoom(),
255 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
256 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
257 window.location.protocol + "//osm.org/go/" :
258 window.location.protocol + "//" + window.location.hostname + "/go/",
259 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
260 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
261 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
262 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
263 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
264 // and drops the last 4 bits of the full 64 bit Morton code.
265 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
269 for (i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
270 digit = (c1 >> (24 - (6 * i))) & 0x3f;
271 str += char_array.charAt(digit);
273 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
274 digit = (c2 >> (24 - (6 * (i - 5)))) & 0x3f;
275 str += char_array.charAt(digit);
277 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
279 // Called to interlace the bits in x and y, making a Morton code.
280 function interlace(x, y) {
281 var interlaced_x = x,
283 interlaced_x = (interlaced_x | (interlaced_x << 8)) & 0x00ff00ff;
284 interlaced_x = (interlaced_x | (interlaced_x << 4)) & 0x0f0f0f0f;
285 interlaced_x = (interlaced_x | (interlaced_x << 2)) & 0x33333333;
286 interlaced_x = (interlaced_x | (interlaced_x << 1)) & 0x55555555;
287 interlaced_y = (interlaced_y | (interlaced_y << 8)) & 0x00ff00ff;
288 interlaced_y = (interlaced_y | (interlaced_y << 4)) & 0x0f0f0f0f;
289 interlaced_y = (interlaced_y | (interlaced_y << 2)) & 0x33333333;
290 interlaced_y = (interlaced_y | (interlaced_y << 1)) & 0x55555555;
291 return (interlaced_x << 1) | interlaced_y;
295 var layers = this.getLayersCode().replace("M", "");
298 params.layers = layers;
301 if (marker && this.hasLayer(marker)) {
306 params[this._object.type] = this._object.id;
309 var query = Qs.stringify(params);
317 getGeoUri: function (marker) {
318 var precision = OSM.zoomPrecision(this.getZoom()),
322 if (marker && this.hasLayer(marker)) {
323 latLng = marker.getLatLng().wrap();
325 latLng = this.getCenter();
328 params.lat = latLng.lat.toFixed(precision);
329 params.lon = latLng.lng.toFixed(precision);
330 params.zoom = this.getZoom();
332 return "geo:" + params.lat + "," + params.lon + "?z=" + params.zoom;
335 addObject: function (object, callback) {
343 var changesetStyle = {
360 if (object.type === "note") {
361 this._objectLoader = {
362 abort: function () {}
365 this._object = object;
366 this._objectLayer = L.featureGroup().addTo(this);
368 L.circleMarker(object.latLng, haloStyle).addTo(this._objectLayer);
371 L.marker(object.latLng, {
375 }).addTo(this._objectLayer);
378 if (callback) callback(this._objectLayer.getBounds());
379 } else { // element or changeset handled by L.OSM.DataLayer
381 this._objectLoader = $.ajax({
382 url: OSM.apiUrl(object),
384 success: function (xml) {
385 map._object = object;
387 map._objectLayer = new L.OSM.DataLayer(null, {
392 changeset: changesetStyle
396 map._objectLayer.interestingNode = function (node, ways, relations) {
397 if (object.type === "node") {
399 } else if (object.type === "relation") {
400 for (var i = 0; i < relations.length; i++) {
401 if (relations[i].members.indexOf(node) !== -1) return true;
408 map._objectLayer.addData(xml);
409 map._objectLayer.addTo(map);
411 if (callback) callback(map._objectLayer.getBounds());
417 removeObject: function () {
419 if (this._objectLoader) this._objectLoader.abort();
420 if (this._objectLayer) this.removeLayer(this._objectLayer);
423 getState: function () {
425 center: this.getCenter().wrap(),
426 zoom: this.getZoom(),
427 layers: this.getLayersCode()
431 setState: function (state, options) {
432 if (state.center) this.setView(state.center, state.zoom, options);
433 if (state.layers) this.updateLayers(state.layers);
436 setSidebarOverlaid: function (overlaid) {
437 var sidebarWidth = 350;
438 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
439 $("#content").addClass("overlay-sidebar");
440 this.invalidateSize({ pan: false });
441 if ($("html").attr("dir") !== "rtl") {
442 this.panBy([-sidebarWidth, 0], { animate: false });
444 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
445 if ($("html").attr("dir") !== "rtl") {
446 this.panBy([sidebarWidth, 0], { animate: false });
448 $("#content").removeClass("overlay-sidebar");
449 this.invalidateSize({ pan: false });
455 L.Icon.Default.imagePath = "/images/";
457 L.Icon.Default.imageUrls = {
458 "/images/marker-icon.png": OSM.MARKER_ICON,
459 "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
460 "/images/marker-shadow.png": OSM.MARKER_SHADOW
463 L.extend(L.Icon.Default.prototype, {
464 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
466 _getIconUrl: function (name) {
467 var url = this._oldGetIconUrl(name);
468 return L.Icon.Default.imageUrls[url];
472 OSM.getUserIcon = function (url) {
474 iconUrl: url || OSM.MARKER_RED,
476 iconAnchor: [12, 41],
477 popupAnchor: [1, -34],
478 shadowUrl: OSM.MARKER_SHADOW,