1 L.extend(L.LatLngBounds.prototype, {
3 return (this._northEast.lat - this._southWest.lat) *
4 (this._northEast.lng - this._southWest.lng);
8 return new L.LatLngBounds(this._southWest.wrap(), this._northEast.wrap());
12 L.OSM.Map = L.Map.extend({
13 initialize: function (id, options) {
14 L.Map.prototype.initialize.call(this, id, options);
18 for (const layerDefinition of OSM.LAYER_DEFINITIONS) {
19 if (layerDefinition.apiKeyId && !OSM[layerDefinition.apiKeyId]) continue;
21 let layerConstructor = L.OSM.TileLayer;
22 const layerOptions = {};
24 for (const [property, value] of Object.entries(layerDefinition)) {
25 if (property === "credit") {
26 layerOptions.attribution = makeAttribution(value);
27 } else if (property === "nameId") {
28 layerOptions.name = I18n.t(`javascripts.map.base.${value}`);
29 } else if (property === "apiKeyId") {
30 layerOptions.apikey = OSM[value];
31 } else if (property === "leafletOsmId") {
32 layerConstructor = L.OSM[value];
33 } else if (property === "leafletOsmDarkId" && OSM.isDarkMap() && L.OSM[value]) {
34 layerConstructor = L.OSM[value];
36 layerOptions[property] = value;
40 const layer = new layerConstructor(layerOptions);
41 layer.on("add", () => {
42 this.fire("baselayerchange", { layer: layer });
44 this.baseLayers.push(layer);
47 this.noteLayer = new L.FeatureGroup();
48 this.noteLayer.options = { code: "N" };
50 this.dataLayer = new L.OSM.DataLayer(null);
51 this.dataLayer.options.code = "D";
53 this.gpsLayer = new L.OSM.GPS({
57 this.gpsLayer.on("add", () => {
58 this.fire("overlayadd", { layer: this.gpsLayer });
59 }).on("remove", () => {
60 this.fire("overlayremove", { layer: this.gpsLayer });
64 this.on("baselayerchange", function (event) {
65 if (this.baseLayers.indexOf(event.layer) >= 0) {
66 this.setMaxZoom(event.layer.options.maxZoom);
70 function makeAttribution(credit) {
73 attribution += I18n.t("javascripts.map.copyright_text", {
74 copyright_link: $("<a>", {
76 text: I18n.t("javascripts.map.openstreetmap_contributors")
80 attribution += credit.donate ? " ♥ " : ". ";
81 attribution += makeCredit(credit);
84 attribution += $("<a>", {
85 href: "https://wiki.osmfoundation.org/wiki/Terms_of_Use",
86 text: I18n.t("javascripts.map.website_and_api_terms")
92 function makeCredit(credit) {
94 for (const childId in credit.children) {
95 children[childId] = makeCredit(credit.children[childId]);
97 const text = I18n.t(`javascripts.map.${credit.id}`, children);
99 const link = $("<a>", {
104 link.addClass("donate-attr");
106 link.attr("target", "_blank");
108 return link.prop("outerHTML");
115 updateLayers: function (layerParam) {
116 const oldBaseLayer = this.getMapBaseLayer();
119 for (const layer of this.baseLayers) {
120 if (!newBaseLayer || layerParam.includes(layer.options.code)) {
121 newBaseLayer = layer;
125 if (newBaseLayer !== oldBaseLayer) {
126 if (oldBaseLayer) this.removeLayer(oldBaseLayer);
127 if (newBaseLayer) this.addLayer(newBaseLayer);
131 getLayersCode: function () {
132 var layerConfig = "";
133 this.eachLayer(function (layer) {
134 if (layer.options && layer.options.code) {
135 layerConfig += layer.options.code;
141 getMapBaseLayerId: function () {
142 const layer = this.getMapBaseLayer();
143 if (layer) return layer.options.layerId;
146 getMapBaseLayer: function () {
147 for (const layer of this.baseLayers) {
148 if (this.hasLayer(layer)) return layer;
152 getUrl: function (marker) {
155 if (marker && this.hasLayer(marker)) {
156 [params.mlat, params.mlon] = OSM.cropLocation(marker.getLatLng(), this.getZoom());
159 var url = window.location.protocol + "//" + OSM.SERVER_URL + "/",
160 query = new URLSearchParams(params),
161 hash = OSM.formatHash(this);
163 if (query) url += "?" + query;
164 if (hash) url += hash;
169 getShortUrl: function (marker) {
170 var zoom = this.getZoom(),
171 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
172 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
173 window.location.protocol + "//osm.org/go/" :
174 window.location.protocol + "//" + window.location.hostname + "/go/",
175 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
176 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
177 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
178 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
179 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
180 // and drops the last 4 bits of the full 64 bit Morton code.
181 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
185 for (i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
186 digit = (c1 >> (24 - (6 * i))) & 0x3f;
187 str += char_array.charAt(digit);
189 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
190 digit = (c2 >> (24 - (6 * (i - 5)))) & 0x3f;
191 str += char_array.charAt(digit);
193 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
195 // Called to interlace the bits in x and y, making a Morton code.
196 function interlace(x, y) {
197 var interlaced_x = x,
199 interlaced_x = (interlaced_x | (interlaced_x << 8)) & 0x00ff00ff;
200 interlaced_x = (interlaced_x | (interlaced_x << 4)) & 0x0f0f0f0f;
201 interlaced_x = (interlaced_x | (interlaced_x << 2)) & 0x33333333;
202 interlaced_x = (interlaced_x | (interlaced_x << 1)) & 0x55555555;
203 interlaced_y = (interlaced_y | (interlaced_y << 8)) & 0x00ff00ff;
204 interlaced_y = (interlaced_y | (interlaced_y << 4)) & 0x0f0f0f0f;
205 interlaced_y = (interlaced_y | (interlaced_y << 2)) & 0x33333333;
206 interlaced_y = (interlaced_y | (interlaced_y << 1)) & 0x55555555;
207 return (interlaced_x << 1) | interlaced_y;
210 const params = new URLSearchParams();
211 var layers = this.getLayersCode().replace("M", "");
214 params.set("layers", layers);
217 if (marker && this.hasLayer(marker)) {
222 params.set(this._object.type, this._object.id);
225 const query = params.toString();
233 getGeoUri: function (marker) {
234 let latLng = this.getCenter();
235 const zoom = this.getZoom();
237 if (marker && this.hasLayer(marker)) {
238 latLng = marker.getLatLng();
241 return `geo:${OSM.cropLocation(latLng, zoom).join(",")}?z=${zoom}`;
244 addObject: function (object, callback) {
252 var changesetStyle = {
269 if (object.type === "note" || object.type === "changeset") {
270 this._objectLoader = {
271 abort: function () {}
274 this._object = object;
275 this._objectLayer = L.featureGroup().addTo(this);
277 if (object.type === "note") {
278 L.circleMarker(object.latLng, haloStyle).addTo(this._objectLayer);
281 L.marker(object.latLng, {
285 }).addTo(this._objectLayer);
287 } else if (object.type === "changeset") {
290 [object.bbox.minlat, object.bbox.minlon],
291 [object.bbox.maxlat, object.bbox.maxlon]
292 ], changesetStyle).addTo(this._objectLayer);
296 if (callback) callback(this._objectLayer.getBounds());
297 this.fire("overlayadd", { layer: this._objectLayer });
298 } else { // element handled by L.OSM.DataLayer
300 this._objectLoader = $.ajax({
301 url: OSM.apiUrl(object),
303 success: function (data) {
304 map._object = object;
306 map._objectLayer = new L.OSM.DataLayer(null, {
311 changeset: changesetStyle
315 map._objectLayer.interestingNode = function (node, wayNodes, relationNodes) {
316 if (object.type === "node") {
318 } else if (object.type === "relation") {
319 return Boolean(relationNodes[node.id]);
325 map._objectLayer.addData(data);
326 map._objectLayer.addTo(map);
328 if (callback) callback(map._objectLayer.getBounds());
329 map.fire("overlayadd", { layer: map._objectLayer });
335 removeObject: function () {
337 if (this._objectLoader) this._objectLoader.abort();
338 if (this._objectLayer) this.removeLayer(this._objectLayer);
339 this.fire("overlayremove", { layer: 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,