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 const oldBaseLayer = this.getMapBaseLayer();
121 for (const layer of this.baseLayers) {
122 if (!newBaseLayer || layerParam.includes(layer.options.code)) {
123 newBaseLayer = layer;
127 if (newBaseLayer !== oldBaseLayer) {
128 if (oldBaseLayer) this.removeLayer(oldBaseLayer);
129 if (newBaseLayer) this.addLayer(newBaseLayer);
133 getLayersCode: function () {
134 var layerConfig = "";
135 this.eachLayer(function (layer) {
136 if (layer.options && layer.options.code) {
137 layerConfig += layer.options.code;
143 getMapBaseLayerId: function () {
144 const layer = this.getMapBaseLayer();
145 if (layer) return layer.options.layerId;
148 getMapBaseLayer: function () {
149 for (const layer of this.baseLayers) {
150 if (this.hasLayer(layer)) return layer;
154 getUrl: function (marker) {
155 var precision = OSM.zoomPrecision(this.getZoom()),
158 if (marker && this.hasLayer(marker)) {
159 var latLng = marker.getLatLng().wrap();
160 params.mlat = latLng.lat.toFixed(precision);
161 params.mlon = latLng.lng.toFixed(precision);
164 var url = window.location.protocol + "//" + OSM.SERVER_URL + "/",
165 query = Qs.stringify(params),
166 hash = OSM.formatHash(this);
168 if (query) url += "?" + query;
169 if (hash) url += hash;
174 getShortUrl: function (marker) {
175 var zoom = this.getZoom(),
176 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
177 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
178 window.location.protocol + "//osm.org/go/" :
179 window.location.protocol + "//" + window.location.hostname + "/go/",
180 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
181 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
182 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
183 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
184 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
185 // and drops the last 4 bits of the full 64 bit Morton code.
186 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
190 for (i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
191 digit = (c1 >> (24 - (6 * i))) & 0x3f;
192 str += char_array.charAt(digit);
194 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
195 digit = (c2 >> (24 - (6 * (i - 5)))) & 0x3f;
196 str += char_array.charAt(digit);
198 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
200 // Called to interlace the bits in x and y, making a Morton code.
201 function interlace(x, y) {
202 var interlaced_x = x,
204 interlaced_x = (interlaced_x | (interlaced_x << 8)) & 0x00ff00ff;
205 interlaced_x = (interlaced_x | (interlaced_x << 4)) & 0x0f0f0f0f;
206 interlaced_x = (interlaced_x | (interlaced_x << 2)) & 0x33333333;
207 interlaced_x = (interlaced_x | (interlaced_x << 1)) & 0x55555555;
208 interlaced_y = (interlaced_y | (interlaced_y << 8)) & 0x00ff00ff;
209 interlaced_y = (interlaced_y | (interlaced_y << 4)) & 0x0f0f0f0f;
210 interlaced_y = (interlaced_y | (interlaced_y << 2)) & 0x33333333;
211 interlaced_y = (interlaced_y | (interlaced_y << 1)) & 0x55555555;
212 return (interlaced_x << 1) | interlaced_y;
216 var layers = this.getLayersCode().replace("M", "");
219 params.layers = layers;
222 if (marker && this.hasLayer(marker)) {
227 params[this._object.type] = this._object.id;
230 var query = Qs.stringify(params);
238 getGeoUri: function (marker) {
239 var precision = OSM.zoomPrecision(this.getZoom()),
243 if (marker && this.hasLayer(marker)) {
244 latLng = marker.getLatLng().wrap();
246 latLng = this.getCenter();
249 params.lat = latLng.lat.toFixed(precision);
250 params.lon = latLng.lng.toFixed(precision);
251 params.zoom = this.getZoom();
253 return "geo:" + params.lat + "," + params.lon + "?z=" + params.zoom;
256 addObject: function (object, callback) {
264 var changesetStyle = {
281 if (object.type === "note" || object.type === "changeset") {
282 this._objectLoader = {
283 abort: function () {}
286 this._object = object;
287 this._objectLayer = L.featureGroup().addTo(this);
289 if (object.type === "note") {
290 L.circleMarker(object.latLng, haloStyle).addTo(this._objectLayer);
293 L.marker(object.latLng, {
297 }).addTo(this._objectLayer);
299 } else if (object.type === "changeset") {
302 [object.bbox.minlat, object.bbox.minlon],
303 [object.bbox.maxlat, object.bbox.maxlon]
304 ], changesetStyle).addTo(this._objectLayer);
308 if (callback) callback(this._objectLayer.getBounds());
309 this.fire("overlayadd", { layer: this._objectLayer });
310 } else { // element handled by L.OSM.DataLayer
312 this._objectLoader = $.ajax({
313 url: OSM.apiUrl(object),
315 success: function (data) {
316 map._object = object;
318 map._objectLayer = new L.OSM.DataLayer(null, {
323 changeset: changesetStyle
327 map._objectLayer.interestingNode = function (node, wayNodes, relationNodes) {
328 if (object.type === "node") {
330 } else if (object.type === "relation") {
331 return Boolean(relationNodes[node.id]);
337 map._objectLayer.addData(data);
338 map._objectLayer.addTo(map);
340 if (callback) callback(map._objectLayer.getBounds());
341 map.fire("overlayadd", { layer: map._objectLayer });
347 removeObject: function () {
349 if (this._objectLoader) this._objectLoader.abort();
350 if (this._objectLayer) this.removeLayer(this._objectLayer);
351 this.fire("overlayremove", { layer: this._objectLayer });
354 getState: function () {
356 center: this.getCenter().wrap(),
357 zoom: this.getZoom(),
358 layers: this.getLayersCode()
362 setState: function (state, options) {
363 if (state.center) this.setView(state.center, state.zoom, options);
364 if (state.layers) this.updateLayers(state.layers);
367 setSidebarOverlaid: function (overlaid) {
368 var sidebarWidth = 350;
369 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
370 $("#content").addClass("overlay-sidebar");
371 this.invalidateSize({ pan: false });
372 if ($("html").attr("dir") !== "rtl") {
373 this.panBy([-sidebarWidth, 0], { animate: false });
375 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
376 if ($("html").attr("dir") !== "rtl") {
377 this.panBy([sidebarWidth, 0], { animate: false });
379 $("#content").removeClass("overlay-sidebar");
380 this.invalidateSize({ pan: false });
386 L.Icon.Default.imagePath = "/images/";
388 L.Icon.Default.imageUrls = {
389 "/images/marker-icon.png": OSM.MARKER_ICON,
390 "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
391 "/images/marker-shadow.png": OSM.MARKER_SHADOW
394 L.extend(L.Icon.Default.prototype, {
395 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
397 _getIconUrl: function (name) {
398 var url = this._oldGetIconUrl(name);
399 return L.Icon.Default.imageUrls[url];
403 OSM.isDarkMap = function () {
404 var mapTheme = $("body").attr("data-map-theme");
405 if (mapTheme) return mapTheme === "dark";
406 var siteTheme = $("html").attr("data-bs-theme");
407 if (siteTheme) return siteTheme === "dark";
408 return window.matchMedia("(prefers-color-scheme: dark)").matches;
411 OSM.getUserIcon = function (url) {
413 iconUrl: url || OSM.MARKER_RED,
415 iconAnchor: [12, 41],
416 popupAnchor: [1, -34],
417 shadowUrl: OSM.MARKER_SHADOW,