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);
20 for (const layerDefinition of OSM.LAYER_DEFINITIONS) {
21 if (layerDefinition.apiKeyId && !OSM[layerDefinition.apiKeyId]) continue;
23 let layerConstructor = L.OSM.TileLayer;
24 const layerOptions = {};
26 for (const [property, value] of Object.entries(layerDefinition)) {
27 if (property === "credit") {
28 layerOptions.attribution = makeAttribution(value);
29 } else if (property === "nameId") {
30 layerOptions.name = I18n.t(`javascripts.map.base.${value}`);
31 } else if (property === "apiKeyId") {
32 layerOptions.apikey = OSM[value];
33 } else if (property === "leafletOsmId") {
34 layerConstructor = L.OSM[value];
35 } else if (property === "leafletOsmDarkId" && OSM.isDarkMap() && L.OSM[value]) {
36 layerConstructor = L.OSM[value];
38 layerOptions[property] = value;
42 const layer = new layerConstructor(layerOptions);
43 layer.on("add", () => {
44 this.fire("baselayerchange", { layer: layer });
46 this.baseLayers.push(layer);
49 this.noteLayer = new L.FeatureGroup();
50 this.noteLayer.options = { code: "N" };
52 this.dataLayer = new L.OSM.DataLayer(null);
53 this.dataLayer.options.code = "D";
55 this.gpsLayer = new L.OSM.GPS({
59 this.gpsLayer.on("add", () => {
60 this.fire("overlayadd", { layer: this.gpsLayer });
61 }).on("remove", () => {
62 this.fire("overlayremove", { layer: this.gpsLayer });
66 this.on("baselayerchange", function (event) {
67 if (this.baseLayers.indexOf(event.layer) >= 0) {
68 this.setMaxZoom(event.layer.options.maxZoom);
72 function makeAttribution(credit) {
75 attribution += I18n.t("javascripts.map.copyright_text", {
76 copyright_link: $("<a>", {
78 text: I18n.t("javascripts.map.openstreetmap_contributors")
82 attribution += credit.donate ? " ♥ " : ". ";
83 attribution += makeCredit(credit);
86 attribution += $("<a>", {
87 href: "https://wiki.osmfoundation.org/wiki/Terms_of_Use",
88 text: I18n.t("javascripts.map.website_and_api_terms")
94 function makeCredit(credit) {
96 for (const childId in credit.children) {
97 children[childId] = makeCredit(credit.children[childId]);
99 const text = I18n.t(`javascripts.map.${credit.id}`, children);
101 const link = $("<a>", {
106 link.addClass("donate-attr");
108 link.attr("target", "_blank");
110 return link.prop("outerHTML");
117 updateLayers: function (layerParam) {
118 var layers = layerParam || "M",
121 for (let i = this.baseLayers.length - 1; i >= 0; i--) {
122 if (layers.indexOf(this.baseLayers[i].options.code) === -1) {
123 this.removeLayer(this.baseLayers[i]);
127 for (let i = this.baseLayers.length - 1; i >= 0; i--) {
128 if (layers.indexOf(this.baseLayers[i].options.code) >= 0) {
129 this.addLayer(this.baseLayers[i]);
130 layersAdded = layersAdded + this.baseLayers[i].options.code;
131 } else if (i === 0 && layersAdded === "") {
132 this.addLayer(this.baseLayers[i]);
137 getLayersCode: function () {
138 var layerConfig = "";
139 this.eachLayer(function (layer) {
140 if (layer.options && layer.options.code) {
141 layerConfig += layer.options.code;
147 getMapBaseLayerId: function () {
148 const layer = this.getMapBaseLayer();
149 if (layer) return layer.options.layerId;
152 getMapBaseLayer: function () {
153 for (const layer of this.baseLayers) {
154 if (this.hasLayer(layer)) return layer;
158 getUrl: function (marker) {
159 var precision = OSM.zoomPrecision(this.getZoom()),
162 if (marker && this.hasLayer(marker)) {
163 var latLng = marker.getLatLng().wrap();
164 params.mlat = latLng.lat.toFixed(precision);
165 params.mlon = latLng.lng.toFixed(precision);
168 var url = window.location.protocol + "//" + OSM.SERVER_URL + "/",
169 query = Qs.stringify(params),
170 hash = OSM.formatHash(this);
172 if (query) url += "?" + query;
173 if (hash) url += hash;
178 getShortUrl: function (marker) {
179 var zoom = this.getZoom(),
180 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
181 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
182 window.location.protocol + "//osm.org/go/" :
183 window.location.protocol + "//" + window.location.hostname + "/go/",
184 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
185 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
186 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
187 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
188 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
189 // and drops the last 4 bits of the full 64 bit Morton code.
190 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
194 for (i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
195 digit = (c1 >> (24 - (6 * i))) & 0x3f;
196 str += char_array.charAt(digit);
198 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
199 digit = (c2 >> (24 - (6 * (i - 5)))) & 0x3f;
200 str += char_array.charAt(digit);
202 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
204 // Called to interlace the bits in x and y, making a Morton code.
205 function interlace(x, y) {
206 var interlaced_x = x,
208 interlaced_x = (interlaced_x | (interlaced_x << 8)) & 0x00ff00ff;
209 interlaced_x = (interlaced_x | (interlaced_x << 4)) & 0x0f0f0f0f;
210 interlaced_x = (interlaced_x | (interlaced_x << 2)) & 0x33333333;
211 interlaced_x = (interlaced_x | (interlaced_x << 1)) & 0x55555555;
212 interlaced_y = (interlaced_y | (interlaced_y << 8)) & 0x00ff00ff;
213 interlaced_y = (interlaced_y | (interlaced_y << 4)) & 0x0f0f0f0f;
214 interlaced_y = (interlaced_y | (interlaced_y << 2)) & 0x33333333;
215 interlaced_y = (interlaced_y | (interlaced_y << 1)) & 0x55555555;
216 return (interlaced_x << 1) | interlaced_y;
220 var layers = this.getLayersCode().replace("M", "");
223 params.layers = layers;
226 if (marker && this.hasLayer(marker)) {
231 params[this._object.type] = this._object.id;
234 var query = Qs.stringify(params);
242 getGeoUri: function (marker) {
243 var precision = OSM.zoomPrecision(this.getZoom()),
247 if (marker && this.hasLayer(marker)) {
248 latLng = marker.getLatLng().wrap();
250 latLng = this.getCenter();
253 params.lat = latLng.lat.toFixed(precision);
254 params.lon = latLng.lng.toFixed(precision);
255 params.zoom = this.getZoom();
257 return "geo:" + params.lat + "," + params.lon + "?z=" + params.zoom;
260 addObject: function (object, callback) {
268 var changesetStyle = {
285 if (object.type === "note" || object.type === "changeset") {
286 this._objectLoader = {
287 abort: function () {}
290 this._object = object;
291 this._objectLayer = L.featureGroup().addTo(this);
293 if (object.type === "note") {
294 L.circleMarker(object.latLng, haloStyle).addTo(this._objectLayer);
297 L.marker(object.latLng, {
301 }).addTo(this._objectLayer);
303 } else if (object.type === "changeset") {
306 [object.bbox.minlat, object.bbox.minlon],
307 [object.bbox.maxlat, object.bbox.maxlon]
308 ], changesetStyle).addTo(this._objectLayer);
312 if (callback) callback(this._objectLayer.getBounds());
313 this.fire("overlayadd", { layer: this._objectLayer });
314 } else { // element handled by L.OSM.DataLayer
316 this._objectLoader = $.ajax({
317 url: OSM.apiUrl(object),
319 success: function (xml) {
320 map._object = object;
322 map._objectLayer = new L.OSM.DataLayer(null, {
327 changeset: changesetStyle
331 map._objectLayer.interestingNode = function (node, wayNodes, relationNodes) {
332 if (object.type === "node") {
334 } else if (object.type === "relation") {
335 return Boolean(relationNodes[node.id]);
341 map._objectLayer.addData(xml);
342 map._objectLayer.addTo(map);
344 if (callback) callback(map._objectLayer.getBounds());
345 map.fire("overlayadd", { layer: map._objectLayer });
351 removeObject: function () {
353 if (this._objectLoader) this._objectLoader.abort();
354 if (this._objectLayer) this.removeLayer(this._objectLayer);
355 this.fire("overlayremove", { layer: this._objectLayer });
358 getState: function () {
360 center: this.getCenter().wrap(),
361 zoom: this.getZoom(),
362 layers: this.getLayersCode()
366 setState: function (state, options) {
367 if (state.center) this.setView(state.center, state.zoom, options);
368 if (state.layers) this.updateLayers(state.layers);
371 setSidebarOverlaid: function (overlaid) {
372 var sidebarWidth = 350;
373 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
374 $("#content").addClass("overlay-sidebar");
375 this.invalidateSize({ pan: false });
376 if ($("html").attr("dir") !== "rtl") {
377 this.panBy([-sidebarWidth, 0], { animate: false });
379 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
380 if ($("html").attr("dir") !== "rtl") {
381 this.panBy([sidebarWidth, 0], { animate: false });
383 $("#content").removeClass("overlay-sidebar");
384 this.invalidateSize({ pan: false });
390 L.Icon.Default.imagePath = "/images/";
392 L.Icon.Default.imageUrls = {
393 "/images/marker-icon.png": OSM.MARKER_ICON,
394 "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
395 "/images/marker-shadow.png": OSM.MARKER_SHADOW
398 L.extend(L.Icon.Default.prototype, {
399 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
401 _getIconUrl: function (name) {
402 var url = this._oldGetIconUrl(name);
403 return L.Icon.Default.imageUrls[url];
407 OSM.isDarkMap = function () {
408 var mapTheme = $("body").attr("data-map-theme");
409 if (mapTheme) return mapTheme === "dark";
410 var siteTheme = $("html").attr("data-bs-theme");
411 if (siteTheme) return siteTheme === "dark";
412 return window.matchMedia("(prefers-color-scheme: dark)").matches;
415 OSM.getUserIcon = function (url) {
417 iconUrl: url || OSM.MARKER_RED,
419 iconAnchor: [12, 41],
420 popupAnchor: [1, -34],
421 shadowUrl: OSM.MARKER_SHADOW,