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 this.baseLayers.push(layer);
46 this.noteLayer = new L.FeatureGroup();
47 this.noteLayer.options = { code: "N" };
49 this.dataLayer = new L.OSM.DataLayer(null);
50 this.dataLayer.options.code = "D";
52 this.gpsLayer = new L.OSM.GPS({
57 this.on("layeradd", function (event) {
58 if (this.baseLayers.indexOf(event.layer) >= 0) {
59 this.setMaxZoom(event.layer.options.maxZoom);
63 function makeAttribution(credit) {
66 attribution += I18n.t("javascripts.map.copyright_text", {
67 copyright_link: $("<a>", {
69 text: I18n.t("javascripts.map.openstreetmap_contributors")
73 attribution += credit.donate ? " ♥ " : ". ";
74 attribution += makeCredit(credit);
77 attribution += $("<a>", {
78 href: "https://wiki.osmfoundation.org/wiki/Terms_of_Use",
79 text: I18n.t("javascripts.map.website_and_api_terms")
85 function makeCredit(credit) {
87 for (const childId in credit.children) {
88 children[childId] = makeCredit(credit.children[childId]);
90 const text = I18n.t(`javascripts.map.${credit.id}`, children);
92 const link = $("<a>", {
97 link.addClass("donate-attr");
99 link.attr("target", "_blank");
101 return link.prop("outerHTML");
108 updateLayers: function (layerParam) {
109 var layers = layerParam || "M",
112 for (var i = this.baseLayers.length - 1; i >= 0; i--) {
113 if (layers.indexOf(this.baseLayers[i].options.code) >= 0) {
114 this.addLayer(this.baseLayers[i]);
115 layersAdded = layersAdded + this.baseLayers[i].options.code;
116 } else if (i === 0 && layersAdded === "") {
117 this.addLayer(this.baseLayers[i]);
119 this.removeLayer(this.baseLayers[i]);
124 getLayersCode: function () {
125 var layerConfig = "";
126 this.eachLayer(function (layer) {
127 if (layer.options && layer.options.code) {
128 layerConfig += layer.options.code;
134 getMapBaseLayerId: function () {
135 const layer = this.getMapBaseLayer();
136 if (layer) return layer.options.layerId;
139 getMapBaseLayer: function () {
140 for (const layer of this.baseLayers) {
141 if (this.hasLayer(layer)) return layer;
145 getUrl: function (marker) {
146 var precision = OSM.zoomPrecision(this.getZoom()),
149 if (marker && this.hasLayer(marker)) {
150 var latLng = marker.getLatLng().wrap();
151 params.mlat = latLng.lat.toFixed(precision);
152 params.mlon = latLng.lng.toFixed(precision);
155 var url = window.location.protocol + "//" + OSM.SERVER_URL + "/",
156 query = Qs.stringify(params),
157 hash = OSM.formatHash(this);
159 if (query) url += "?" + query;
160 if (hash) url += hash;
165 getShortUrl: function (marker) {
166 var zoom = this.getZoom(),
167 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
168 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
169 window.location.protocol + "//osm.org/go/" :
170 window.location.protocol + "//" + window.location.hostname + "/go/",
171 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
172 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
173 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
174 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
175 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
176 // and drops the last 4 bits of the full 64 bit Morton code.
177 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
181 for (i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
182 digit = (c1 >> (24 - (6 * i))) & 0x3f;
183 str += char_array.charAt(digit);
185 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
186 digit = (c2 >> (24 - (6 * (i - 5)))) & 0x3f;
187 str += char_array.charAt(digit);
189 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
191 // Called to interlace the bits in x and y, making a Morton code.
192 function interlace(x, y) {
193 var interlaced_x = x,
195 interlaced_x = (interlaced_x | (interlaced_x << 8)) & 0x00ff00ff;
196 interlaced_x = (interlaced_x | (interlaced_x << 4)) & 0x0f0f0f0f;
197 interlaced_x = (interlaced_x | (interlaced_x << 2)) & 0x33333333;
198 interlaced_x = (interlaced_x | (interlaced_x << 1)) & 0x55555555;
199 interlaced_y = (interlaced_y | (interlaced_y << 8)) & 0x00ff00ff;
200 interlaced_y = (interlaced_y | (interlaced_y << 4)) & 0x0f0f0f0f;
201 interlaced_y = (interlaced_y | (interlaced_y << 2)) & 0x33333333;
202 interlaced_y = (interlaced_y | (interlaced_y << 1)) & 0x55555555;
203 return (interlaced_x << 1) | interlaced_y;
207 var layers = this.getLayersCode().replace("M", "");
210 params.layers = layers;
213 if (marker && this.hasLayer(marker)) {
218 params[this._object.type] = this._object.id;
221 var query = Qs.stringify(params);
229 getGeoUri: function (marker) {
230 var precision = OSM.zoomPrecision(this.getZoom()),
234 if (marker && this.hasLayer(marker)) {
235 latLng = marker.getLatLng().wrap();
237 latLng = this.getCenter();
240 params.lat = latLng.lat.toFixed(precision);
241 params.lon = latLng.lng.toFixed(precision);
242 params.zoom = this.getZoom();
244 return "geo:" + params.lat + "," + params.lon + "?z=" + params.zoom;
247 addObject: function (object, callback) {
255 var changesetStyle = {
272 if (object.type === "note" || object.type === "changeset") {
273 this._objectLoader = {
274 abort: function () {}
277 this._object = object;
278 this._objectLayer = L.featureGroup().addTo(this);
280 if (object.type === "note") {
281 L.circleMarker(object.latLng, haloStyle).addTo(this._objectLayer);
284 L.marker(object.latLng, {
288 }).addTo(this._objectLayer);
290 } else if (object.type === "changeset") {
293 [object.bbox.minlat, object.bbox.minlon],
294 [object.bbox.maxlat, object.bbox.maxlon]
295 ], changesetStyle).addTo(this._objectLayer);
299 if (callback) callback(this._objectLayer.getBounds());
300 } else { // element handled by L.OSM.DataLayer
302 this._objectLoader = $.ajax({
303 url: OSM.apiUrl(object),
305 success: function (xml) {
306 map._object = object;
308 map._objectLayer = new L.OSM.DataLayer(null, {
313 changeset: changesetStyle
317 map._objectLayer.interestingNode = function (node, wayNodes, relationNodes) {
318 if (object.type === "node") {
320 } else if (object.type === "relation") {
321 return Boolean(relationNodes[node.id]);
327 map._objectLayer.addData(xml);
328 map._objectLayer.addTo(map);
330 if (callback) callback(map._objectLayer.getBounds());
336 removeObject: function () {
338 if (this._objectLoader) this._objectLoader.abort();
339 if (this._objectLayer) this.removeLayer(this._objectLayer);
342 getState: function () {
344 center: this.getCenter().wrap(),
345 zoom: this.getZoom(),
346 layers: this.getLayersCode()
350 setState: function (state, options) {
351 if (state.center) this.setView(state.center, state.zoom, options);
352 if (state.layers) this.updateLayers(state.layers);
355 setSidebarOverlaid: function (overlaid) {
356 var sidebarWidth = 350;
357 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
358 $("#content").addClass("overlay-sidebar");
359 this.invalidateSize({ pan: false });
360 if ($("html").attr("dir") !== "rtl") {
361 this.panBy([-sidebarWidth, 0], { animate: false });
363 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
364 if ($("html").attr("dir") !== "rtl") {
365 this.panBy([sidebarWidth, 0], { animate: false });
367 $("#content").removeClass("overlay-sidebar");
368 this.invalidateSize({ pan: false });
374 L.Icon.Default.imagePath = "/images/";
376 L.Icon.Default.imageUrls = {
377 "/images/marker-icon.png": OSM.MARKER_ICON,
378 "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
379 "/images/marker-shadow.png": OSM.MARKER_SHADOW
382 L.extend(L.Icon.Default.prototype, {
383 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
385 _getIconUrl: function (name) {
386 var url = this._oldGetIconUrl(name);
387 return L.Icon.Default.imageUrls[url];
391 OSM.isDarkMap = function () {
392 var mapTheme = $("body").attr("data-map-theme");
393 if (mapTheme) return mapTheme === "dark";
394 var siteTheme = $("html").attr("data-bs-theme");
395 if (siteTheme) return siteTheme === "dark";
396 return window.matchMedia("(prefers-color-scheme: dark)").matches;
399 OSM.getUserIcon = function (url) {
401 iconUrl: url || OSM.MARKER_RED,
403 iconAnchor: [12, 41],
404 popupAnchor: [1, -34],
405 shadowUrl: OSM.MARKER_SHADOW,