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);
18 const layerCredits = {
20 id: "make_a_donation",
21 href: "https://supporting.openstreetmap.org",
29 href: "https://www.cyclosm.org"
33 href: "https://openstreetmap.fr/"
38 id: "thunderforest_credit",
42 href: "https://www.thunderforest.com/"
47 id: "tracestrack_credit",
51 href: "https://www.tracestrack.com/"
60 href: "https://www.hotosm.org/"
64 href: "https://openstreetmap.fr/"
72 this.baseLayers.push(new L.OSM.Mapnik({
73 attribution: makeAttribution("mapnik"),
76 name: I18n.t("javascripts.map.base.standard")
79 this.baseLayers.push(new L.OSM.CyclOSM({
80 attribution: makeAttribution("cyclosm"),
83 name: I18n.t("javascripts.map.base.cyclosm")
86 if (OSM.THUNDERFOREST_KEY) {
87 this.baseLayers.push(new L.OSM.CycleMap({
88 attribution: makeAttribution("thunderforest"),
89 apikey: OSM.THUNDERFOREST_KEY,
92 name: I18n.t("javascripts.map.base.cycle_map")
95 this.baseLayers.push(new L.OSM.TransportMap({
96 attribution: makeAttribution("thunderforest"),
97 apikey: OSM.THUNDERFOREST_KEY,
99 keyid: "transportmap",
100 name: I18n.t("javascripts.map.base.transport_map")
104 if (OSM.TRACESTRACK_KEY) {
105 this.baseLayers.push(new L.OSM.TracestrackTopo({
106 attribution: makeAttribution("tracestrack"),
107 apikey: OSM.TRACESTRACK_KEY,
109 keyid: "tracestracktopo",
110 name: I18n.t("javascripts.map.base.tracestracktop_topo")
114 this.baseLayers.push(new L.OSM.HOT({
115 attribution: makeAttribution("hotosm"),
118 name: I18n.t("javascripts.map.base.hot")
121 this.noteLayer = new L.FeatureGroup();
122 this.noteLayer.options = { code: "N" };
124 this.dataLayer = new L.OSM.DataLayer(null);
125 this.dataLayer.options.code = "D";
127 this.gpsLayer = new L.OSM.GPS({
132 this.on("layeradd", function (event) {
133 if (this.baseLayers.indexOf(event.layer) >= 0) {
134 this.setMaxZoom(event.layer.options.maxZoom);
138 function makeAttribution(id) {
139 const layerCredit = layerCredits[id];
140 let attribution = "";
142 attribution += I18n.t("javascripts.map.copyright_text", {
143 copyright_link: $("<a>", {
145 text: I18n.t("javascripts.map.openstreetmap_contributors")
149 attribution += layerCredit.donate ? " ♥ " : ". ";
150 attribution += makeCredit(layerCredit);
153 attribution += $("<a>", {
154 href: "https://wiki.osmfoundation.org/wiki/Terms_of_Use",
155 text: I18n.t("javascripts.map.website_and_api_terms")
156 }).prop("outerHTML");
161 function makeCredit(credit) {
163 for (const childId in credit.children) {
164 children[childId] = makeCredit(credit.children[childId]);
166 const text = I18n.t(`javascripts.map.${credit.id}`, children);
168 const link = $("<a>", {
173 link.addClass("donate-attr");
175 link.attr("target", "_blank");
177 return link.prop("outerHTML");
184 updateLayers: function (layerParam) {
185 var layers = layerParam || "M",
188 for (var i = this.baseLayers.length - 1; i >= 0; i--) {
189 if (layers.indexOf(this.baseLayers[i].options.code) >= 0) {
190 this.addLayer(this.baseLayers[i]);
191 layersAdded = layersAdded + this.baseLayers[i].options.code;
192 } else if (i === 0 && layersAdded === "") {
193 this.addLayer(this.baseLayers[i]);
195 this.removeLayer(this.baseLayers[i]);
200 getLayersCode: function () {
201 var layerConfig = "";
202 this.eachLayer(function (layer) {
203 if (layer.options && layer.options.code) {
204 layerConfig += layer.options.code;
210 getMapBaseLayerId: function () {
212 this.eachLayer(function (layer) {
213 if (layer.options && layer.options.keyid) baseLayerId = layer.options.keyid;
218 getUrl: function (marker) {
219 var precision = OSM.zoomPrecision(this.getZoom()),
222 if (marker && this.hasLayer(marker)) {
223 var latLng = marker.getLatLng().wrap();
224 params.mlat = latLng.lat.toFixed(precision);
225 params.mlon = latLng.lng.toFixed(precision);
228 var url = window.location.protocol + "//" + OSM.SERVER_URL + "/",
229 query = Qs.stringify(params),
230 hash = OSM.formatHash(this);
232 if (query) url += "?" + query;
233 if (hash) url += hash;
238 getShortUrl: function (marker) {
239 var zoom = this.getZoom(),
240 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
241 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
242 window.location.protocol + "//osm.org/go/" :
243 window.location.protocol + "//" + window.location.hostname + "/go/",
244 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
245 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
246 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
247 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
248 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
249 // and drops the last 4 bits of the full 64 bit Morton code.
250 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
254 for (i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
255 digit = (c1 >> (24 - (6 * i))) & 0x3f;
256 str += char_array.charAt(digit);
258 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
259 digit = (c2 >> (24 - (6 * (i - 5)))) & 0x3f;
260 str += char_array.charAt(digit);
262 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
264 // Called to interlace the bits in x and y, making a Morton code.
265 function interlace(x, y) {
266 var interlaced_x = x,
268 interlaced_x = (interlaced_x | (interlaced_x << 8)) & 0x00ff00ff;
269 interlaced_x = (interlaced_x | (interlaced_x << 4)) & 0x0f0f0f0f;
270 interlaced_x = (interlaced_x | (interlaced_x << 2)) & 0x33333333;
271 interlaced_x = (interlaced_x | (interlaced_x << 1)) & 0x55555555;
272 interlaced_y = (interlaced_y | (interlaced_y << 8)) & 0x00ff00ff;
273 interlaced_y = (interlaced_y | (interlaced_y << 4)) & 0x0f0f0f0f;
274 interlaced_y = (interlaced_y | (interlaced_y << 2)) & 0x33333333;
275 interlaced_y = (interlaced_y | (interlaced_y << 1)) & 0x55555555;
276 return (interlaced_x << 1) | interlaced_y;
280 var layers = this.getLayersCode().replace("M", "");
283 params.layers = layers;
286 if (marker && this.hasLayer(marker)) {
291 params[this._object.type] = this._object.id;
294 var query = Qs.stringify(params);
302 getGeoUri: function (marker) {
303 var precision = OSM.zoomPrecision(this.getZoom()),
307 if (marker && this.hasLayer(marker)) {
308 latLng = marker.getLatLng().wrap();
310 latLng = this.getCenter();
313 params.lat = latLng.lat.toFixed(precision);
314 params.lon = latLng.lng.toFixed(precision);
315 params.zoom = this.getZoom();
317 return "geo:" + params.lat + "," + params.lon + "?z=" + params.zoom;
320 addObject: function (object, callback) {
328 var changesetStyle = {
345 if (object.type === "note") {
346 this._objectLoader = {
347 abort: function () {}
350 this._object = object;
351 this._objectLayer = L.featureGroup().addTo(this);
353 L.circleMarker(object.latLng, haloStyle).addTo(this._objectLayer);
356 L.marker(object.latLng, {
360 }).addTo(this._objectLayer);
363 if (callback) callback(this._objectLayer.getBounds());
364 } else { // element or changeset handled by L.OSM.DataLayer
366 this._objectLoader = $.ajax({
367 url: OSM.apiUrl(object),
369 success: function (xml) {
370 map._object = object;
372 map._objectLayer = new L.OSM.DataLayer(null, {
377 changeset: changesetStyle
381 map._objectLayer.interestingNode = function (node, ways, relations) {
382 if (object.type === "node") {
384 } else if (object.type === "relation") {
385 for (var i = 0; i < relations.length; i++) {
386 if (relations[i].members.indexOf(node) !== -1) return true;
393 map._objectLayer.addData(xml);
394 map._objectLayer.addTo(map);
396 if (callback) callback(map._objectLayer.getBounds());
402 removeObject: function () {
404 if (this._objectLoader) this._objectLoader.abort();
405 if (this._objectLayer) this.removeLayer(this._objectLayer);
408 getState: function () {
410 center: this.getCenter().wrap(),
411 zoom: this.getZoom(),
412 layers: this.getLayersCode()
416 setState: function (state, options) {
417 if (state.center) this.setView(state.center, state.zoom, options);
418 if (state.layers) this.updateLayers(state.layers);
421 setSidebarOverlaid: function (overlaid) {
422 var sidebarWidth = 350;
423 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
424 $("#content").addClass("overlay-sidebar");
425 this.invalidateSize({ pan: false });
426 if ($("html").attr("dir") !== "rtl") {
427 this.panBy([-sidebarWidth, 0], { animate: false });
429 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
430 if ($("html").attr("dir") !== "rtl") {
431 this.panBy([sidebarWidth, 0], { animate: false });
433 $("#content").removeClass("overlay-sidebar");
434 this.invalidateSize({ pan: false });
440 L.Icon.Default.imagePath = "/images/";
442 L.Icon.Default.imageUrls = {
443 "/images/marker-icon.png": OSM.MARKER_ICON,
444 "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
445 "/images/marker-shadow.png": OSM.MARKER_SHADOW
448 L.extend(L.Icon.Default.prototype, {
449 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
451 _getIconUrl: function (name) {
452 var url = this._oldGetIconUrl(name);
453 return L.Icon.Default.imageUrls[url];
457 OSM.getUserIcon = function (url) {
459 iconUrl: url || OSM.MARKER_RED,
461 iconAnchor: [12, 41],
462 popupAnchor: [1, -34],
463 shadowUrl: OSM.MARKER_SHADOW,