1 L.extend(L.LatLngBounds.prototype, {
3 return (this._northEast.lat - this._southWest.lat) *
4 (this._northEast.lng - this._southWest.lng);
8 L.OSM.Map = L.Map.extend({
9 initialize: function (id, options) {
10 L.Map.prototype.initialize.call(this, id, options);
12 this.baseLayers = OSM.LAYER_DEFINITIONS.map((
13 { credit, nameId, leafletOsmId, leafletOsmDarkId, ...layerOptions }
15 if (credit) layerOptions.attribution = makeAttribution(credit);
16 if (nameId) layerOptions.name = OSM.i18n.t(`javascripts.map.base.${nameId}`);
17 const layerConstructor =
18 (OSM.isDarkMap() && L.OSM[leafletOsmDarkId]) ||
19 L.OSM[leafletOsmId] ||
22 const layer = new layerConstructor(layerOptions);
23 layer.on("add", () => {
24 this.fire("baselayerchange", { layer: layer });
29 this.noteLayer = new L.FeatureGroup();
30 this.noteLayer.options = { code: "N" };
32 this.dataLayer = new L.OSM.DataLayer(null);
33 this.dataLayer.options.code = "D";
35 this.gpsLayer = new L.OSM.GPS({
39 this.gpsLayer.on("add", () => {
40 this.fire("overlayadd", { layer: this.gpsLayer });
41 }).on("remove", () => {
42 this.fire("overlayremove", { layer: this.gpsLayer });
45 this.on("baselayerchange", function (event) {
46 if (this.baseLayers.indexOf(event.layer) >= 0) {
47 this.setMaxZoom(event.layer.options.maxZoom);
51 function makeAttribution(credit) {
54 attribution += OSM.i18n.t("javascripts.map.copyright_text", {
55 copyright_link: $("<a>", {
57 text: OSM.i18n.t("javascripts.map.openstreetmap_contributors")
61 attribution += credit.donate ? " ♥ " : ". ";
62 attribution += makeCredit(credit);
65 attribution += $("<a>", {
66 href: "https://wiki.osmfoundation.org/wiki/Terms_of_Use",
67 text: OSM.i18n.t("javascripts.map.website_and_api_terms")
73 function makeCredit(credit) {
75 for (const childId in credit.children) {
76 children[childId] = makeCredit(credit.children[childId]);
78 const text = OSM.i18n.t(`javascripts.map.${credit.id}`, children);
80 const link = $("<a>", {
85 link.addClass("donate-attr");
87 link.attr("target", "_blank");
89 return link.prop("outerHTML");
95 updateLayers: function (layerParam) {
96 const oldBaseLayer = this.getMapBaseLayer();
99 for (const layer of this.baseLayers) {
100 if (!newBaseLayer || layerParam.includes(layer.options.code)) {
101 newBaseLayer = layer;
105 if (newBaseLayer !== oldBaseLayer) {
106 if (oldBaseLayer) this.removeLayer(oldBaseLayer);
107 if (newBaseLayer) this.addLayer(newBaseLayer);
111 getLayersCode: function () {
112 let layerConfig = "";
113 this.eachLayer(function (layer) {
114 if (layer.options && layer.options.code) {
115 layerConfig += layer.options.code;
121 getMapBaseLayerId: function () {
122 const layer = this.getMapBaseLayer();
123 if (layer) return layer.options.layerId;
126 getMapBaseLayer: function () {
127 for (const layer of this.baseLayers) {
128 if (this.hasLayer(layer)) return layer;
132 getUrl: function (marker) {
135 if (marker && this.hasLayer(marker)) {
136 [params.mlat, params.mlon] = OSM.cropLocation(marker.getLatLng(), this.getZoom());
139 let url = location.protocol + "//" + OSM.SERVER_URL + "/";
140 const query = new URLSearchParams(params),
141 hash = OSM.formatHash(this);
143 if (query) url += "?" + query;
144 if (hash) url += hash;
149 getShortUrl: function (marker) {
150 const zoom = this.getZoom(),
151 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
152 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
153 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
154 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
155 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
156 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
157 // and drops the last 4 bits of the full 64 bit Morton code.
158 c1 = interlace(x >>> 17, y >>> 17),
159 c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
160 let str = location.protocol + "//" + location.hostname.replace(/^www\.openstreetmap\.org/i, "osm.org") + "/go/";
162 for (let i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
163 const digit = (c1 >> (24 - (6 * i))) & 0x3f;
164 str += char_array.charAt(digit);
166 for (let i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
167 const digit = (c2 >> (24 - (6 * (i - 5)))) & 0x3f;
168 str += char_array.charAt(digit);
170 for (let i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
172 // Called to interlace the bits in x and y, making a Morton code.
173 function interlace(x, y) {
174 let interlaced_x = x,
176 interlaced_x = (interlaced_x | (interlaced_x << 8)) & 0x00ff00ff;
177 interlaced_x = (interlaced_x | (interlaced_x << 4)) & 0x0f0f0f0f;
178 interlaced_x = (interlaced_x | (interlaced_x << 2)) & 0x33333333;
179 interlaced_x = (interlaced_x | (interlaced_x << 1)) & 0x55555555;
180 interlaced_y = (interlaced_y | (interlaced_y << 8)) & 0x00ff00ff;
181 interlaced_y = (interlaced_y | (interlaced_y << 4)) & 0x0f0f0f0f;
182 interlaced_y = (interlaced_y | (interlaced_y << 2)) & 0x33333333;
183 interlaced_y = (interlaced_y | (interlaced_y << 1)) & 0x55555555;
184 return (interlaced_x << 1) | interlaced_y;
187 const params = new URLSearchParams();
188 const layers = this.getLayersCode().replace("M", "");
191 params.set("layers", layers);
194 if (marker && this.hasLayer(marker)) {
199 params.set(this._object.type, this._object.id);
202 const query = params.toString();
210 getGeoUri: function (marker) {
211 let latLng = this.getCenter();
212 const zoom = this.getZoom();
214 if (marker && this.hasLayer(marker)) {
215 latLng = marker.getLatLng();
218 return `geo:${OSM.cropLocation(latLng, zoom).join(",")}?z=${zoom}`;
221 addObject: function (object, callback) {
222 const objectStyle = {
229 const changesetStyle = {
246 if (object.type === "note" || object.type === "changeset") {
247 this._objectLoader = { abort: () => {} };
249 this._object = object;
250 this._objectLayer = L.featureGroup().addTo(this);
252 if (object.type === "note") {
253 L.circleMarker(object.latLng, haloStyle).addTo(this._objectLayer);
256 L.marker(object.latLng, {
260 }).addTo(this._objectLayer);
262 } else if (object.type === "changeset") {
265 [object.bbox.minlat, object.bbox.minlon],
266 [object.bbox.maxlat, object.bbox.maxlon]
267 ], changesetStyle).addTo(this._objectLayer);
271 if (callback) callback(this._objectLayer.getBounds());
272 this.fire("overlayadd", { layer: this._objectLayer });
273 } else { // element handled by L.OSM.DataLayer
275 this._objectLoader = new AbortController();
276 fetch(OSM.apiUrl(object), {
277 headers: { accept: "application/json" },
278 signal: this._objectLoader.signal
280 .then(response => response.json())
281 .then(function (data) {
282 map._object = object;
284 map._objectLayer = new L.OSM.DataLayer(null, {
289 changeset: changesetStyle
293 map._objectLayer.interestingNode = function (node, wayNodes, relationNodes) {
294 return object.type === "node" ||
295 (object.type === "relation" && Boolean(relationNodes[node.id]));
298 map._objectLayer.addData(data);
299 map._objectLayer.addTo(map);
301 if (callback) callback(map._objectLayer.getBounds());
302 map.fire("overlayadd", { layer: map._objectLayer });
308 removeObject: function () {
310 if (this._objectLoader) this._objectLoader.abort();
311 if (this._objectLayer) this.removeLayer(this._objectLayer);
312 this.fire("overlayremove", { layer: this._objectLayer });
315 getState: function () {
317 center: this.getCenter().wrap(),
318 zoom: this.getZoom(),
319 layers: this.getLayersCode()
323 setState: function (state, options) {
324 if (state.center) this.setView(state.center, state.zoom, options);
325 if (state.layers) this.updateLayers(state.layers);
328 setSidebarOverlaid: function (overlaid) {
329 const mediumDeviceWidth = window.getComputedStyle(document.documentElement).getPropertyValue("--bs-breakpoint-md");
330 const isMediumDevice = window.matchMedia(`(max-width: ${mediumDeviceWidth})`).matches;
331 const sidebarWidth = $("#sidebar").width();
332 const sidebarHeight = $("#sidebar").height();
333 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
334 $("#content").addClass("overlay-sidebar");
335 this.invalidateSize({ pan: false });
336 if (isMediumDevice) {
337 this.panBy([0, -sidebarHeight], { animate: false });
338 } else if ($("html").attr("dir") !== "rtl") {
339 this.panBy([-sidebarWidth, 0], { animate: false });
341 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
342 if (isMediumDevice) {
343 this.panBy([0, $("#map").height() / 2], { animate: false });
344 } else if ($("html").attr("dir") !== "rtl") {
345 this.panBy([sidebarWidth, 0], { animate: false });
347 $("#content").removeClass("overlay-sidebar");
348 this.invalidateSize({ pan: false });
354 OSM.isDarkMap = function () {
355 const mapTheme = $("body").attr("data-map-theme");
356 if (mapTheme) return mapTheme === "dark";
357 const siteTheme = $("html").attr("data-bs-theme");
358 if (siteTheme) return siteTheme === "dark";
359 return window.matchMedia("(prefers-color-scheme: dark)").matches;
362 OSM.getMarker = function ({ icon = "dot", color = "var(--marker-red)", shadow = true }) {
363 const html = `<svg viewBox="0 0 25 40" class="pe-none"${
364 shadow ? " overflow='visible'" : ""
366 shadow ? "<use href='#pin-shadow' />" : ""
367 }<use href="#pin-${icon}" color="${color}" class="pe-auto" /></svg>`;
371 iconAnchor: [12.5, 40],
372 popupAnchor: [1, -34]
377 "closed": OSM.getMarker({ icon: "tick", color: "var(--marker-green)", shadow: false }),
378 "new": OSM.getMarker({ icon: "plus", color: "var(--marker-blue)", shadow: false }),
379 "open": OSM.getMarker({ icon: "cross", color: "var(--marker-red)", shadow: false })