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];
36 layerOptions[property] = value;
40 const layer = new layerConstructor(layerOptions);
41 this.baseLayers.push(layer);
44 this.noteLayer = new L.FeatureGroup();
45 this.noteLayer.options = { code: "N" };
47 this.dataLayer = new L.OSM.DataLayer(null);
48 this.dataLayer.options.code = "D";
50 this.gpsLayer = new L.OSM.GPS({
55 this.on("layeradd", function (event) {
56 if (this.baseLayers.indexOf(event.layer) >= 0) {
57 this.setMaxZoom(event.layer.options.maxZoom);
61 function makeAttribution(credit) {
64 attribution += I18n.t("javascripts.map.copyright_text", {
65 copyright_link: $("<a>", {
67 text: I18n.t("javascripts.map.openstreetmap_contributors")
71 attribution += credit.donate ? " ♥ " : ". ";
72 attribution += makeCredit(credit);
75 attribution += $("<a>", {
76 href: "https://wiki.osmfoundation.org/wiki/Terms_of_Use",
77 text: I18n.t("javascripts.map.website_and_api_terms")
83 function makeCredit(credit) {
85 for (const childId in credit.children) {
86 children[childId] = makeCredit(credit.children[childId]);
88 const text = I18n.t(`javascripts.map.${credit.id}`, children);
90 const link = $("<a>", {
95 link.addClass("donate-attr");
97 link.attr("target", "_blank");
99 return link.prop("outerHTML");
106 updateLayers: function (layerParam) {
107 var layers = layerParam || "M",
110 for (var i = this.baseLayers.length - 1; i >= 0; i--) {
111 if (layers.indexOf(this.baseLayers[i].options.code) >= 0) {
112 this.addLayer(this.baseLayers[i]);
113 layersAdded = layersAdded + this.baseLayers[i].options.code;
114 } else if (i === 0 && layersAdded === "") {
115 this.addLayer(this.baseLayers[i]);
117 this.removeLayer(this.baseLayers[i]);
122 getLayersCode: function () {
123 var layerConfig = "";
124 this.eachLayer(function (layer) {
125 if (layer.options && layer.options.code) {
126 layerConfig += layer.options.code;
132 getMapBaseLayerId: function () {
133 const layer = this.getMapBaseLayer();
134 if (layer) return layer.options.layerId;
137 getMapBaseLayer: function () {
138 for (const layer of this.baseLayers) {
139 if (this.hasLayer(layer)) return layer;
143 getUrl: function (marker) {
144 var precision = OSM.zoomPrecision(this.getZoom()),
147 if (marker && this.hasLayer(marker)) {
148 var latLng = marker.getLatLng().wrap();
149 params.mlat = latLng.lat.toFixed(precision);
150 params.mlon = latLng.lng.toFixed(precision);
153 var url = window.location.protocol + "//" + OSM.SERVER_URL + "/",
154 query = Qs.stringify(params),
155 hash = OSM.formatHash(this);
157 if (query) url += "?" + query;
158 if (hash) url += hash;
163 getShortUrl: function (marker) {
164 var zoom = this.getZoom(),
165 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
166 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
167 window.location.protocol + "//osm.org/go/" :
168 window.location.protocol + "//" + window.location.hostname + "/go/",
169 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
170 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
171 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
172 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
173 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
174 // and drops the last 4 bits of the full 64 bit Morton code.
175 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
179 for (i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
180 digit = (c1 >> (24 - (6 * i))) & 0x3f;
181 str += char_array.charAt(digit);
183 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
184 digit = (c2 >> (24 - (6 * (i - 5)))) & 0x3f;
185 str += char_array.charAt(digit);
187 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
189 // Called to interlace the bits in x and y, making a Morton code.
190 function interlace(x, y) {
191 var interlaced_x = x,
193 interlaced_x = (interlaced_x | (interlaced_x << 8)) & 0x00ff00ff;
194 interlaced_x = (interlaced_x | (interlaced_x << 4)) & 0x0f0f0f0f;
195 interlaced_x = (interlaced_x | (interlaced_x << 2)) & 0x33333333;
196 interlaced_x = (interlaced_x | (interlaced_x << 1)) & 0x55555555;
197 interlaced_y = (interlaced_y | (interlaced_y << 8)) & 0x00ff00ff;
198 interlaced_y = (interlaced_y | (interlaced_y << 4)) & 0x0f0f0f0f;
199 interlaced_y = (interlaced_y | (interlaced_y << 2)) & 0x33333333;
200 interlaced_y = (interlaced_y | (interlaced_y << 1)) & 0x55555555;
201 return (interlaced_x << 1) | interlaced_y;
205 var layers = this.getLayersCode().replace("M", "");
208 params.layers = layers;
211 if (marker && this.hasLayer(marker)) {
216 params[this._object.type] = this._object.id;
219 var query = Qs.stringify(params);
227 getGeoUri: function (marker) {
228 var precision = OSM.zoomPrecision(this.getZoom()),
232 if (marker && this.hasLayer(marker)) {
233 latLng = marker.getLatLng().wrap();
235 latLng = this.getCenter();
238 params.lat = latLng.lat.toFixed(precision);
239 params.lon = latLng.lng.toFixed(precision);
240 params.zoom = this.getZoom();
242 return "geo:" + params.lat + "," + params.lon + "?z=" + params.zoom;
245 addObject: function (object, callback) {
253 var changesetStyle = {
270 if (object.type === "note") {
271 this._objectLoader = {
272 abort: function () {}
275 this._object = object;
276 this._objectLayer = L.featureGroup().addTo(this);
278 L.circleMarker(object.latLng, haloStyle).addTo(this._objectLayer);
281 L.marker(object.latLng, {
285 }).addTo(this._objectLayer);
288 if (callback) callback(this._objectLayer.getBounds());
289 } else { // element or changeset handled by L.OSM.DataLayer
291 this._objectLoader = $.ajax({
292 url: OSM.apiUrl(object),
294 success: function (xml) {
295 map._object = object;
297 map._objectLayer = new L.OSM.DataLayer(null, {
302 changeset: changesetStyle
306 map._objectLayer.interestingNode = function (node, ways, relations) {
307 if (object.type === "node") {
309 } else if (object.type === "relation") {
310 for (var i = 0; i < relations.length; i++) {
311 if (relations[i].members.indexOf(node) !== -1) return true;
318 map._objectLayer.addData(xml);
319 map._objectLayer.addTo(map);
321 if (callback) callback(map._objectLayer.getBounds());
327 removeObject: function () {
329 if (this._objectLoader) this._objectLoader.abort();
330 if (this._objectLayer) this.removeLayer(this._objectLayer);
333 getState: function () {
335 center: this.getCenter().wrap(),
336 zoom: this.getZoom(),
337 layers: this.getLayersCode()
341 setState: function (state, options) {
342 if (state.center) this.setView(state.center, state.zoom, options);
343 if (state.layers) this.updateLayers(state.layers);
346 setSidebarOverlaid: function (overlaid) {
347 var sidebarWidth = 350;
348 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
349 $("#content").addClass("overlay-sidebar");
350 this.invalidateSize({ pan: false });
351 if ($("html").attr("dir") !== "rtl") {
352 this.panBy([-sidebarWidth, 0], { animate: false });
354 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
355 if ($("html").attr("dir") !== "rtl") {
356 this.panBy([sidebarWidth, 0], { animate: false });
358 $("#content").removeClass("overlay-sidebar");
359 this.invalidateSize({ pan: false });
365 L.Icon.Default.imagePath = "/images/";
367 L.Icon.Default.imageUrls = {
368 "/images/marker-icon.png": OSM.MARKER_ICON,
369 "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
370 "/images/marker-shadow.png": OSM.MARKER_SHADOW
373 L.extend(L.Icon.Default.prototype, {
374 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
376 _getIconUrl: function (name) {
377 var url = this._oldGetIconUrl(name);
378 return L.Icon.Default.imageUrls[url];
382 OSM.getUserIcon = function (url) {
384 iconUrl: url || OSM.MARKER_RED,
386 iconAnchor: [12, 41],
387 popupAnchor: [1, -34],
388 shadowUrl: OSM.MARKER_SHADOW,