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 });
46 this.on("baselayerchange", function (event) {
47 if (this.baseLayers.indexOf(event.layer) >= 0) {
48 this.setMaxZoom(event.layer.options.maxZoom);
52 function makeAttribution(credit) {
55 attribution += OSM.i18n.t("javascripts.map.copyright_text", {
56 copyright_link: $("<a>", {
58 text: OSM.i18n.t("javascripts.map.openstreetmap_contributors")
62 attribution += credit.donate ? " ♥ " : ". ";
63 attribution += makeCredit(credit);
66 attribution += $("<a>", {
67 href: "https://wiki.osmfoundation.org/wiki/Terms_of_Use",
68 text: OSM.i18n.t("javascripts.map.website_and_api_terms")
74 function makeCredit(credit) {
76 for (const childId in credit.children) {
77 children[childId] = makeCredit(credit.children[childId]);
79 const text = OSM.i18n.t(`javascripts.map.${credit.id}`, children);
81 const link = $("<a>", {
86 link.addClass("donate-attr");
88 link.attr("target", "_blank");
90 return link.prop("outerHTML");
96 updateLayers: function (layerParam) {
97 const oldBaseLayer = this.getMapBaseLayer();
100 for (const layer of this.baseLayers) {
101 if (!newBaseLayer || layerParam.includes(layer.options.code)) {
102 newBaseLayer = layer;
106 if (newBaseLayer !== oldBaseLayer) {
107 if (oldBaseLayer) this.removeLayer(oldBaseLayer);
108 if (newBaseLayer) this.addLayer(newBaseLayer);
112 getLayersCode: function () {
113 let layerConfig = "";
114 this.eachLayer(function (layer) {
115 if (layer.options && layer.options.code) {
116 layerConfig += layer.options.code;
122 getMapBaseLayerId: function () {
123 const layer = this.getMapBaseLayer();
124 if (layer) return layer.options.layerId;
127 getMapBaseLayer: function () {
128 for (const layer of this.baseLayers) {
129 if (this.hasLayer(layer)) return layer;
133 getUrl: function (marker) {
136 if (marker && this.hasLayer(marker)) {
137 [params.mlat, params.mlon] = OSM.cropLocation(marker.getLatLng(), this.getZoom());
140 let url = location.protocol + "//" + OSM.SERVER_URL + "/";
141 const query = new URLSearchParams(params),
142 hash = OSM.formatHash(this);
144 if (query) url += "?" + query;
145 if (hash) url += hash;
150 getShortUrl: function (marker) {
151 const zoom = this.getZoom(),
152 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
153 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
154 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
155 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
156 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
157 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
158 // and drops the last 4 bits of the full 64 bit Morton code.
159 c1 = interlace(x >>> 17, y >>> 17),
160 c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
161 let str = location.protocol + "//" + location.hostname.replace(/^www\.openstreetmap\.org/i, "osm.org") + "/go/";
163 for (let i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
164 const digit = (c1 >> (24 - (6 * i))) & 0x3f;
165 str += char_array.charAt(digit);
167 for (let i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
168 const digit = (c2 >> (24 - (6 * (i - 5)))) & 0x3f;
169 str += char_array.charAt(digit);
171 for (let i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
173 // Called to interlace the bits in x and y, making a Morton code.
174 function interlace(x, y) {
175 let interlaced_x = x,
177 interlaced_x = (interlaced_x | (interlaced_x << 8)) & 0x00ff00ff;
178 interlaced_x = (interlaced_x | (interlaced_x << 4)) & 0x0f0f0f0f;
179 interlaced_x = (interlaced_x | (interlaced_x << 2)) & 0x33333333;
180 interlaced_x = (interlaced_x | (interlaced_x << 1)) & 0x55555555;
181 interlaced_y = (interlaced_y | (interlaced_y << 8)) & 0x00ff00ff;
182 interlaced_y = (interlaced_y | (interlaced_y << 4)) & 0x0f0f0f0f;
183 interlaced_y = (interlaced_y | (interlaced_y << 2)) & 0x33333333;
184 interlaced_y = (interlaced_y | (interlaced_y << 1)) & 0x55555555;
185 return (interlaced_x << 1) | interlaced_y;
188 const params = new URLSearchParams();
189 const layers = this.getLayersCode().replace("M", "");
192 params.set("layers", layers);
195 if (marker && this.hasLayer(marker)) {
200 params.set(this._object.type, this._object.id);
203 const query = params.toString();
211 getGeoUri: function (marker) {
212 let latLng = this.getCenter();
213 const zoom = this.getZoom();
215 if (marker && this.hasLayer(marker)) {
216 latLng = marker.getLatLng();
219 return `geo:${OSM.cropLocation(latLng, zoom).join(",")}?z=${zoom}`;
222 addObject: function (object, callback) {
223 const objectStyle = {
230 const changesetStyle = {
247 if (object.type === "note" || object.type === "changeset") {
248 this._objectLoader = { abort: () => {} };
250 this._object = object;
251 this._objectLayer = L.featureGroup().addTo(this);
253 if (object.type === "note") {
254 L.circleMarker(object.latLng, haloStyle).addTo(this._objectLayer);
257 L.marker(object.latLng, {
261 }).addTo(this._objectLayer);
263 } else if (object.type === "changeset") {
266 [object.bbox.minlat, object.bbox.minlon],
267 [object.bbox.maxlat, object.bbox.maxlon]
268 ], changesetStyle).addTo(this._objectLayer);
272 if (callback) callback(this._objectLayer.getBounds());
273 this.fire("overlayadd", { layer: this._objectLayer });
274 } else { // element handled by L.OSM.DataLayer
276 this._objectLoader = new AbortController();
277 fetch(OSM.apiUrl(object), {
278 headers: { accept: "application/json" },
279 signal: this._objectLoader.signal
281 .then(response => response.json())
282 .then(function (data) {
283 map._object = object;
285 map._objectLayer = new L.OSM.DataLayer(null, {
290 changeset: changesetStyle
294 map._objectLayer.interestingNode = function (node, wayNodes, relationNodes) {
295 return object.type === "node" ||
296 (object.type === "relation" && Boolean(relationNodes[node.id]));
299 map._objectLayer.addData(data);
300 map._objectLayer.addTo(map);
302 if (callback) callback(map._objectLayer.getBounds());
303 map.fire("overlayadd", { layer: map._objectLayer });
309 removeObject: function () {
311 if (this._objectLoader) this._objectLoader.abort();
312 if (this._objectLayer) this.removeLayer(this._objectLayer);
313 this.fire("overlayremove", { layer: this._objectLayer });
316 getState: function () {
318 center: this.getCenter().wrap(),
319 zoom: this.getZoom(),
320 layers: this.getLayersCode()
324 setState: function (state, options) {
325 if (state.center) this.setView(state.center, state.zoom, options);
326 if (state.layers) this.updateLayers(state.layers);
329 setSidebarOverlaid: function (overlaid) {
330 const mediumDeviceWidth = window.getComputedStyle(document.documentElement).getPropertyValue("--bs-breakpoint-md");
331 const isMediumDevice = window.matchMedia(`(max-width: ${mediumDeviceWidth})`).matches;
332 const sidebarWidth = $("#sidebar").width();
333 const sidebarHeight = $("#sidebar").height();
334 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
335 $("#content").addClass("overlay-sidebar");
336 this.invalidateSize({ pan: false });
337 if (isMediumDevice) {
338 this.panBy([0, -sidebarHeight], { animate: false });
339 } else if ($("html").attr("dir") !== "rtl") {
340 this.panBy([-sidebarWidth, 0], { animate: false });
342 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
343 if (isMediumDevice) {
344 this.panBy([0, $("#map").height() / 2], { animate: false });
345 } else if ($("html").attr("dir") !== "rtl") {
346 this.panBy([sidebarWidth, 0], { animate: false });
348 $("#content").removeClass("overlay-sidebar");
349 this.invalidateSize({ pan: false });
355 L.Icon.Default.imagePath = "/images/";
357 L.Icon.Default.imageUrls = {
358 "/images/marker-icon.png": OSM.MARKER_ICON,
359 "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
360 "/images/marker-shadow.png": OSM.MARKER_SHADOW
363 L.extend(L.Icon.Default.prototype, {
364 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
366 _getIconUrl: function (name) {
367 const url = this._oldGetIconUrl(name);
368 return L.Icon.Default.imageUrls[url];
372 OSM.isDarkMap = function () {
373 const mapTheme = $("body").attr("data-map-theme");
374 if (mapTheme) return mapTheme === "dark";
375 const siteTheme = $("html").attr("data-bs-theme");
376 if (siteTheme) return siteTheme === "dark";
377 return window.matchMedia("(prefers-color-scheme: dark)").matches;
380 OSM.getMarker = function ({ icon = "MARKER_RED", shadow = true, height = 41 }) {
382 iconUrl: OSM[icon.toUpperCase()] || OSM.MARKER_RED,
383 iconSize: [25, height],
384 iconAnchor: [12, height],
385 popupAnchor: [1, -34]
388 options.shadowUrl = OSM.MARKER_SHADOW;
389 options.shadowSize = [41, 41];
390 options.shadowAnchor = [12, 41];
392 return L.icon(options);