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 === "keyId") {
30 layerOptions.keyid = value;
31 } else if (property === "nameId") {
32 layerOptions.name = I18n.t(`javascripts.map.base.${value}`);
33 } else if (property === "apiKeyId") {
34 layerOptions.apikey = OSM[value];
35 } else if (property === "leafletOsmId") {
36 layerConstructor = L.OSM[value];
38 layerOptions[property] = value;
42 const layer = new layerConstructor(layerOptions);
43 this.baseLayers.push(layer);
46 this.noteLayer = new L.FeatureGroup();
47 this.noteLayer.options = { code: "N" };
49 this.dataLayer = new L.OSM.DataLayer(null);
50 this.dataLayer.options.code = "D";
52 this.gpsLayer = new L.OSM.GPS({
57 this.on("layeradd", function (event) {
58 if (this.baseLayers.indexOf(event.layer) >= 0) {
59 this.setMaxZoom(event.layer.options.maxZoom);
63 function makeAttribution(credit) {
66 attribution += I18n.t("javascripts.map.copyright_text", {
67 copyright_link: $("<a>", {
69 text: I18n.t("javascripts.map.openstreetmap_contributors")
73 attribution += credit.donate ? " ♥ " : ". ";
74 attribution += makeCredit(credit);
77 attribution += $("<a>", {
78 href: "https://wiki.osmfoundation.org/wiki/Terms_of_Use",
79 text: I18n.t("javascripts.map.website_and_api_terms")
85 function makeCredit(credit) {
87 for (const childId in credit.children) {
88 children[childId] = makeCredit(credit.children[childId]);
90 const text = I18n.t(`javascripts.map.${credit.id}`, children);
92 const link = $("<a>", {
97 link.addClass("donate-attr");
99 link.attr("target", "_blank");
101 return link.prop("outerHTML");
108 updateLayers: function (layerParam) {
109 var layers = layerParam || "M",
112 for (var i = this.baseLayers.length - 1; i >= 0; i--) {
113 if (layers.indexOf(this.baseLayers[i].options.code) >= 0) {
114 this.addLayer(this.baseLayers[i]);
115 layersAdded = layersAdded + this.baseLayers[i].options.code;
116 } else if (i === 0 && layersAdded === "") {
117 this.addLayer(this.baseLayers[i]);
119 this.removeLayer(this.baseLayers[i]);
124 getLayersCode: function () {
125 var layerConfig = "";
126 this.eachLayer(function (layer) {
127 if (layer.options && layer.options.code) {
128 layerConfig += layer.options.code;
134 getMapBaseLayerId: function () {
136 this.eachLayer(function (layer) {
137 if (layer.options && layer.options.keyid) baseLayerId = layer.options.keyid;
142 getUrl: function (marker) {
143 var precision = OSM.zoomPrecision(this.getZoom()),
146 if (marker && this.hasLayer(marker)) {
147 var latLng = marker.getLatLng().wrap();
148 params.mlat = latLng.lat.toFixed(precision);
149 params.mlon = latLng.lng.toFixed(precision);
152 var url = window.location.protocol + "//" + OSM.SERVER_URL + "/",
153 query = Qs.stringify(params),
154 hash = OSM.formatHash(this);
156 if (query) url += "?" + query;
157 if (hash) url += hash;
162 getShortUrl: function (marker) {
163 var zoom = this.getZoom(),
164 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
165 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
166 window.location.protocol + "//osm.org/go/" :
167 window.location.protocol + "//" + window.location.hostname + "/go/",
168 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
169 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
170 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
171 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
172 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
173 // and drops the last 4 bits of the full 64 bit Morton code.
174 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
178 for (i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
179 digit = (c1 >> (24 - (6 * i))) & 0x3f;
180 str += char_array.charAt(digit);
182 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
183 digit = (c2 >> (24 - (6 * (i - 5)))) & 0x3f;
184 str += char_array.charAt(digit);
186 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
188 // Called to interlace the bits in x and y, making a Morton code.
189 function interlace(x, y) {
190 var interlaced_x = x,
192 interlaced_x = (interlaced_x | (interlaced_x << 8)) & 0x00ff00ff;
193 interlaced_x = (interlaced_x | (interlaced_x << 4)) & 0x0f0f0f0f;
194 interlaced_x = (interlaced_x | (interlaced_x << 2)) & 0x33333333;
195 interlaced_x = (interlaced_x | (interlaced_x << 1)) & 0x55555555;
196 interlaced_y = (interlaced_y | (interlaced_y << 8)) & 0x00ff00ff;
197 interlaced_y = (interlaced_y | (interlaced_y << 4)) & 0x0f0f0f0f;
198 interlaced_y = (interlaced_y | (interlaced_y << 2)) & 0x33333333;
199 interlaced_y = (interlaced_y | (interlaced_y << 1)) & 0x55555555;
200 return (interlaced_x << 1) | interlaced_y;
204 var layers = this.getLayersCode().replace("M", "");
207 params.layers = layers;
210 if (marker && this.hasLayer(marker)) {
215 params[this._object.type] = this._object.id;
218 var query = Qs.stringify(params);
226 getGeoUri: function (marker) {
227 var precision = OSM.zoomPrecision(this.getZoom()),
231 if (marker && this.hasLayer(marker)) {
232 latLng = marker.getLatLng().wrap();
234 latLng = this.getCenter();
237 params.lat = latLng.lat.toFixed(precision);
238 params.lon = latLng.lng.toFixed(precision);
239 params.zoom = this.getZoom();
241 return "geo:" + params.lat + "," + params.lon + "?z=" + params.zoom;
244 addObject: function (object, callback) {
252 var changesetStyle = {
269 if (object.type === "note") {
270 this._objectLoader = {
271 abort: function () {}
274 this._object = object;
275 this._objectLayer = L.featureGroup().addTo(this);
277 L.circleMarker(object.latLng, haloStyle).addTo(this._objectLayer);
280 L.marker(object.latLng, {
284 }).addTo(this._objectLayer);
287 if (callback) callback(this._objectLayer.getBounds());
288 } else { // element or changeset handled by L.OSM.DataLayer
290 this._objectLoader = $.ajax({
291 url: OSM.apiUrl(object),
293 success: function (xml) {
294 map._object = object;
296 map._objectLayer = new L.OSM.DataLayer(null, {
301 changeset: changesetStyle
305 map._objectLayer.interestingNode = function (node, ways, relations) {
306 if (object.type === "node") {
308 } else if (object.type === "relation") {
309 for (var i = 0; i < relations.length; i++) {
310 if (relations[i].members.indexOf(node) !== -1) return true;
317 map._objectLayer.addData(xml);
318 map._objectLayer.addTo(map);
320 if (callback) callback(map._objectLayer.getBounds());
326 removeObject: function () {
328 if (this._objectLoader) this._objectLoader.abort();
329 if (this._objectLayer) this.removeLayer(this._objectLayer);
332 getState: function () {
334 center: this.getCenter().wrap(),
335 zoom: this.getZoom(),
336 layers: this.getLayersCode()
340 setState: function (state, options) {
341 if (state.center) this.setView(state.center, state.zoom, options);
342 if (state.layers) this.updateLayers(state.layers);
345 setSidebarOverlaid: function (overlaid) {
346 var sidebarWidth = 350;
347 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
348 $("#content").addClass("overlay-sidebar");
349 this.invalidateSize({ pan: false });
350 if ($("html").attr("dir") !== "rtl") {
351 this.panBy([-sidebarWidth, 0], { animate: false });
353 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
354 if ($("html").attr("dir") !== "rtl") {
355 this.panBy([sidebarWidth, 0], { animate: false });
357 $("#content").removeClass("overlay-sidebar");
358 this.invalidateSize({ pan: false });
364 L.Icon.Default.imagePath = "/images/";
366 L.Icon.Default.imageUrls = {
367 "/images/marker-icon.png": OSM.MARKER_ICON,
368 "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
369 "/images/marker-shadow.png": OSM.MARKER_SHADOW
372 L.extend(L.Icon.Default.prototype, {
373 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
375 _getIconUrl: function (name) {
376 var url = this._oldGetIconUrl(name);
377 return L.Icon.Default.imageUrls[url];
381 OSM.getUserIcon = function (url) {
383 iconUrl: url || OSM.MARKER_RED,
385 iconAnchor: [12, 41],
386 popupAnchor: [1, -34],
387 shadowUrl: OSM.MARKER_SHADOW,