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 const layerOptions = {
24 attribution: makeAttribution(layerDefinition.credit),
25 code: layerDefinition.code,
26 keyid: layerDefinition.keyId,
27 name: I18n.t(`javascripts.map.base.${layerDefinition.nameId}`)
29 if (layerDefinition.apiKeyId) {
30 layerOptions.apikey = OSM[layerDefinition.apiKeyId];
33 const layer = new L.OSM[layerDefinition.leafletOsmId](layerOptions);
34 this.baseLayers.push(layer);
37 this.noteLayer = new L.FeatureGroup();
38 this.noteLayer.options = { code: "N" };
40 this.dataLayer = new L.OSM.DataLayer(null);
41 this.dataLayer.options.code = "D";
43 this.gpsLayer = new L.OSM.GPS({
48 this.on("layeradd", function (event) {
49 if (this.baseLayers.indexOf(event.layer) >= 0) {
50 this.setMaxZoom(event.layer.options.maxZoom);
54 function makeAttribution(credit) {
57 attribution += I18n.t("javascripts.map.copyright_text", {
58 copyright_link: $("<a>", {
60 text: I18n.t("javascripts.map.openstreetmap_contributors")
64 attribution += credit.donate ? " ♥ " : ". ";
65 attribution += makeCredit(credit);
68 attribution += $("<a>", {
69 href: "https://wiki.osmfoundation.org/wiki/Terms_of_Use",
70 text: I18n.t("javascripts.map.website_and_api_terms")
76 function makeCredit(credit) {
78 for (const childId in credit.children) {
79 children[childId] = makeCredit(credit.children[childId]);
81 const text = I18n.t(`javascripts.map.${credit.id}`, children);
83 const link = $("<a>", {
88 link.addClass("donate-attr");
90 link.attr("target", "_blank");
92 return link.prop("outerHTML");
99 updateLayers: function (layerParam) {
100 var layers = layerParam || "M",
103 for (var i = this.baseLayers.length - 1; i >= 0; i--) {
104 if (layers.indexOf(this.baseLayers[i].options.code) >= 0) {
105 this.addLayer(this.baseLayers[i]);
106 layersAdded = layersAdded + this.baseLayers[i].options.code;
107 } else if (i === 0 && layersAdded === "") {
108 this.addLayer(this.baseLayers[i]);
110 this.removeLayer(this.baseLayers[i]);
115 getLayersCode: function () {
116 var layerConfig = "";
117 this.eachLayer(function (layer) {
118 if (layer.options && layer.options.code) {
119 layerConfig += layer.options.code;
125 getMapBaseLayerId: function () {
127 this.eachLayer(function (layer) {
128 if (layer.options && layer.options.keyid) baseLayerId = layer.options.keyid;
133 getUrl: function (marker) {
134 var precision = OSM.zoomPrecision(this.getZoom()),
137 if (marker && this.hasLayer(marker)) {
138 var latLng = marker.getLatLng().wrap();
139 params.mlat = latLng.lat.toFixed(precision);
140 params.mlon = latLng.lng.toFixed(precision);
143 var url = window.location.protocol + "//" + OSM.SERVER_URL + "/",
144 query = Qs.stringify(params),
145 hash = OSM.formatHash(this);
147 if (query) url += "?" + query;
148 if (hash) url += hash;
153 getShortUrl: function (marker) {
154 var zoom = this.getZoom(),
155 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
156 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
157 window.location.protocol + "//osm.org/go/" :
158 window.location.protocol + "//" + window.location.hostname + "/go/",
159 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
160 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
161 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
162 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
163 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
164 // and drops the last 4 bits of the full 64 bit Morton code.
165 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
169 for (i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
170 digit = (c1 >> (24 - (6 * i))) & 0x3f;
171 str += char_array.charAt(digit);
173 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
174 digit = (c2 >> (24 - (6 * (i - 5)))) & 0x3f;
175 str += char_array.charAt(digit);
177 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
179 // Called to interlace the bits in x and y, making a Morton code.
180 function interlace(x, y) {
181 var interlaced_x = x,
183 interlaced_x = (interlaced_x | (interlaced_x << 8)) & 0x00ff00ff;
184 interlaced_x = (interlaced_x | (interlaced_x << 4)) & 0x0f0f0f0f;
185 interlaced_x = (interlaced_x | (interlaced_x << 2)) & 0x33333333;
186 interlaced_x = (interlaced_x | (interlaced_x << 1)) & 0x55555555;
187 interlaced_y = (interlaced_y | (interlaced_y << 8)) & 0x00ff00ff;
188 interlaced_y = (interlaced_y | (interlaced_y << 4)) & 0x0f0f0f0f;
189 interlaced_y = (interlaced_y | (interlaced_y << 2)) & 0x33333333;
190 interlaced_y = (interlaced_y | (interlaced_y << 1)) & 0x55555555;
191 return (interlaced_x << 1) | interlaced_y;
195 var layers = this.getLayersCode().replace("M", "");
198 params.layers = layers;
201 if (marker && this.hasLayer(marker)) {
206 params[this._object.type] = this._object.id;
209 var query = Qs.stringify(params);
217 getGeoUri: function (marker) {
218 var precision = OSM.zoomPrecision(this.getZoom()),
222 if (marker && this.hasLayer(marker)) {
223 latLng = marker.getLatLng().wrap();
225 latLng = this.getCenter();
228 params.lat = latLng.lat.toFixed(precision);
229 params.lon = latLng.lng.toFixed(precision);
230 params.zoom = this.getZoom();
232 return "geo:" + params.lat + "," + params.lon + "?z=" + params.zoom;
235 addObject: function (object, callback) {
243 var changesetStyle = {
260 if (object.type === "note") {
261 this._objectLoader = {
262 abort: function () {}
265 this._object = object;
266 this._objectLayer = L.featureGroup().addTo(this);
268 L.circleMarker(object.latLng, haloStyle).addTo(this._objectLayer);
271 L.marker(object.latLng, {
275 }).addTo(this._objectLayer);
278 if (callback) callback(this._objectLayer.getBounds());
279 } else { // element or changeset handled by L.OSM.DataLayer
281 this._objectLoader = $.ajax({
282 url: OSM.apiUrl(object),
284 success: function (xml) {
285 map._object = object;
287 map._objectLayer = new L.OSM.DataLayer(null, {
292 changeset: changesetStyle
296 map._objectLayer.interestingNode = function (node, ways, relations) {
297 if (object.type === "node") {
299 } else if (object.type === "relation") {
300 for (var i = 0; i < relations.length; i++) {
301 if (relations[i].members.indexOf(node) !== -1) return true;
308 map._objectLayer.addData(xml);
309 map._objectLayer.addTo(map);
311 if (callback) callback(map._objectLayer.getBounds());
317 removeObject: function () {
319 if (this._objectLoader) this._objectLoader.abort();
320 if (this._objectLayer) this.removeLayer(this._objectLayer);
323 getState: function () {
325 center: this.getCenter().wrap(),
326 zoom: this.getZoom(),
327 layers: this.getLayersCode()
331 setState: function (state, options) {
332 if (state.center) this.setView(state.center, state.zoom, options);
333 if (state.layers) this.updateLayers(state.layers);
336 setSidebarOverlaid: function (overlaid) {
337 var sidebarWidth = 350;
338 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
339 $("#content").addClass("overlay-sidebar");
340 this.invalidateSize({ pan: false });
341 if ($("html").attr("dir") !== "rtl") {
342 this.panBy([-sidebarWidth, 0], { animate: false });
344 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
345 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 var url = this._oldGetIconUrl(name);
368 return L.Icon.Default.imageUrls[url];
372 OSM.getUserIcon = function (url) {
374 iconUrl: url || OSM.MARKER_RED,
376 iconAnchor: [12, 41],
377 popupAnchor: [1, -34],
378 shadowUrl: OSM.MARKER_SHADOW,