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 var layers = layerParam || "M";
120 for (let i = this.baseLayers.length - 1; i >= 0; i--) {
121 if (layers.indexOf(this.baseLayers[i].options.code) === -1) {
122 this.removeLayer(this.baseLayers[i]);
126 for (let i = this.baseLayers.length - 1; i >= 0; i--) {
127 if (layers.indexOf(this.baseLayers[i].options.code) >= 0 || i === 0) {
128 this.addLayer(this.baseLayers[i]);
134 getLayersCode: function () {
135 var layerConfig = "";
136 this.eachLayer(function (layer) {
137 if (layer.options && layer.options.code) {
138 layerConfig += layer.options.code;
144 getMapBaseLayerId: function () {
145 const layer = this.getMapBaseLayer();
146 if (layer) return layer.options.layerId;
149 getMapBaseLayer: function () {
150 for (const layer of this.baseLayers) {
151 if (this.hasLayer(layer)) return layer;
155 getUrl: function (marker) {
158 if (marker && this.hasLayer(marker)) {
159 [params.mlat, params.mlon] = OSM.cropLocation(marker.getLatLng(), this.getZoom());
162 var url = window.location.protocol + "//" + OSM.SERVER_URL + "/",
163 query = Qs.stringify(params),
164 hash = OSM.formatHash(this);
166 if (query) url += "?" + query;
167 if (hash) url += hash;
172 getShortUrl: function (marker) {
173 var zoom = this.getZoom(),
174 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
175 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
176 window.location.protocol + "//osm.org/go/" :
177 window.location.protocol + "//" + window.location.hostname + "/go/",
178 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
179 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
180 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
181 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
182 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
183 // and drops the last 4 bits of the full 64 bit Morton code.
184 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
188 for (i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
189 digit = (c1 >> (24 - (6 * i))) & 0x3f;
190 str += char_array.charAt(digit);
192 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
193 digit = (c2 >> (24 - (6 * (i - 5)))) & 0x3f;
194 str += char_array.charAt(digit);
196 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
198 // Called to interlace the bits in x and y, making a Morton code.
199 function interlace(x, y) {
200 var interlaced_x = x,
202 interlaced_x = (interlaced_x | (interlaced_x << 8)) & 0x00ff00ff;
203 interlaced_x = (interlaced_x | (interlaced_x << 4)) & 0x0f0f0f0f;
204 interlaced_x = (interlaced_x | (interlaced_x << 2)) & 0x33333333;
205 interlaced_x = (interlaced_x | (interlaced_x << 1)) & 0x55555555;
206 interlaced_y = (interlaced_y | (interlaced_y << 8)) & 0x00ff00ff;
207 interlaced_y = (interlaced_y | (interlaced_y << 4)) & 0x0f0f0f0f;
208 interlaced_y = (interlaced_y | (interlaced_y << 2)) & 0x33333333;
209 interlaced_y = (interlaced_y | (interlaced_y << 1)) & 0x55555555;
210 return (interlaced_x << 1) | interlaced_y;
214 var layers = this.getLayersCode().replace("M", "");
217 params.layers = layers;
220 if (marker && this.hasLayer(marker)) {
225 params[this._object.type] = this._object.id;
228 var query = Qs.stringify(params);
236 getGeoUri: function (marker) {
237 let latLng = this.getCenter();
238 const zoom = this.getZoom();
240 if (marker && this.hasLayer(marker)) {
241 latLng = marker.getLatLng();
244 return `geo:${OSM.cropLocation(latLng, zoom).join(",")}?z=${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 this.fire("overlayadd", { layer: this._objectLayer });
301 } else { // element handled by L.OSM.DataLayer
303 this._objectLoader = $.ajax({
304 url: OSM.apiUrl(object),
306 success: function (data) {
307 map._object = object;
309 map._objectLayer = new L.OSM.DataLayer(null, {
314 changeset: changesetStyle
318 map._objectLayer.interestingNode = function (node, wayNodes, relationNodes) {
319 if (object.type === "node") {
321 } else if (object.type === "relation") {
322 return Boolean(relationNodes[node.id]);
328 map._objectLayer.addData(data);
329 map._objectLayer.addTo(map);
331 if (callback) callback(map._objectLayer.getBounds());
332 map.fire("overlayadd", { layer: map._objectLayer });
338 removeObject: function () {
340 if (this._objectLoader) this._objectLoader.abort();
341 if (this._objectLayer) this.removeLayer(this._objectLayer);
342 this.fire("overlayremove", { layer: this._objectLayer });
345 getState: function () {
347 center: this.getCenter().wrap(),
348 zoom: this.getZoom(),
349 layers: this.getLayersCode()
353 setState: function (state, options) {
354 if (state.center) this.setView(state.center, state.zoom, options);
355 if (state.layers) this.updateLayers(state.layers);
358 setSidebarOverlaid: function (overlaid) {
359 var sidebarWidth = 350;
360 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
361 $("#content").addClass("overlay-sidebar");
362 this.invalidateSize({ pan: false });
363 if ($("html").attr("dir") !== "rtl") {
364 this.panBy([-sidebarWidth, 0], { animate: false });
366 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
367 if ($("html").attr("dir") !== "rtl") {
368 this.panBy([sidebarWidth, 0], { animate: false });
370 $("#content").removeClass("overlay-sidebar");
371 this.invalidateSize({ pan: false });
377 L.Icon.Default.imagePath = "/images/";
379 L.Icon.Default.imageUrls = {
380 "/images/marker-icon.png": OSM.MARKER_ICON,
381 "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
382 "/images/marker-shadow.png": OSM.MARKER_SHADOW
385 L.extend(L.Icon.Default.prototype, {
386 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
388 _getIconUrl: function (name) {
389 var url = this._oldGetIconUrl(name);
390 return L.Icon.Default.imageUrls[url];
394 OSM.isDarkMap = function () {
395 var mapTheme = $("body").attr("data-map-theme");
396 if (mapTheme) return mapTheme === "dark";
397 var siteTheme = $("html").attr("data-bs-theme");
398 if (siteTheme) return siteTheme === "dark";
399 return window.matchMedia("(prefers-color-scheme: dark)").matches;
402 OSM.getUserIcon = function (url) {
404 iconUrl: url || OSM.MARKER_RED,
406 iconAnchor: [12, 41],
407 popupAnchor: [1, -34],
408 shadowUrl: OSM.MARKER_SHADOW,