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 () {
134 this.eachLayer(function (layer) {
135 if (layer.options && layer.options.layerId) baseLayerId = layer.options.layerId;
140 getUrl: function (marker) {
141 var precision = OSM.zoomPrecision(this.getZoom()),
144 if (marker && this.hasLayer(marker)) {
145 var latLng = marker.getLatLng().wrap();
146 params.mlat = latLng.lat.toFixed(precision);
147 params.mlon = latLng.lng.toFixed(precision);
150 var url = window.location.protocol + "//" + OSM.SERVER_URL + "/",
151 query = Qs.stringify(params),
152 hash = OSM.formatHash(this);
154 if (query) url += "?" + query;
155 if (hash) url += hash;
160 getShortUrl: function (marker) {
161 var zoom = this.getZoom(),
162 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
163 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
164 window.location.protocol + "//osm.org/go/" :
165 window.location.protocol + "//" + window.location.hostname + "/go/",
166 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
167 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
168 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
169 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
170 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
171 // and drops the last 4 bits of the full 64 bit Morton code.
172 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
176 for (i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
177 digit = (c1 >> (24 - (6 * i))) & 0x3f;
178 str += char_array.charAt(digit);
180 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
181 digit = (c2 >> (24 - (6 * (i - 5)))) & 0x3f;
182 str += char_array.charAt(digit);
184 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
186 // Called to interlace the bits in x and y, making a Morton code.
187 function interlace(x, y) {
188 var interlaced_x = x,
190 interlaced_x = (interlaced_x | (interlaced_x << 8)) & 0x00ff00ff;
191 interlaced_x = (interlaced_x | (interlaced_x << 4)) & 0x0f0f0f0f;
192 interlaced_x = (interlaced_x | (interlaced_x << 2)) & 0x33333333;
193 interlaced_x = (interlaced_x | (interlaced_x << 1)) & 0x55555555;
194 interlaced_y = (interlaced_y | (interlaced_y << 8)) & 0x00ff00ff;
195 interlaced_y = (interlaced_y | (interlaced_y << 4)) & 0x0f0f0f0f;
196 interlaced_y = (interlaced_y | (interlaced_y << 2)) & 0x33333333;
197 interlaced_y = (interlaced_y | (interlaced_y << 1)) & 0x55555555;
198 return (interlaced_x << 1) | interlaced_y;
202 var layers = this.getLayersCode().replace("M", "");
205 params.layers = layers;
208 if (marker && this.hasLayer(marker)) {
213 params[this._object.type] = this._object.id;
216 var query = Qs.stringify(params);
224 getGeoUri: function (marker) {
225 var precision = OSM.zoomPrecision(this.getZoom()),
229 if (marker && this.hasLayer(marker)) {
230 latLng = marker.getLatLng().wrap();
232 latLng = this.getCenter();
235 params.lat = latLng.lat.toFixed(precision);
236 params.lon = latLng.lng.toFixed(precision);
237 params.zoom = this.getZoom();
239 return "geo:" + params.lat + "," + params.lon + "?z=" + params.zoom;
242 addObject: function (object, callback) {
250 var changesetStyle = {
267 if (object.type === "note") {
268 this._objectLoader = {
269 abort: function () {}
272 this._object = object;
273 this._objectLayer = L.featureGroup().addTo(this);
275 L.circleMarker(object.latLng, haloStyle).addTo(this._objectLayer);
278 L.marker(object.latLng, {
282 }).addTo(this._objectLayer);
285 if (callback) callback(this._objectLayer.getBounds());
286 } else { // element or changeset handled by L.OSM.DataLayer
288 this._objectLoader = $.ajax({
289 url: OSM.apiUrl(object),
291 success: function (xml) {
292 map._object = object;
294 map._objectLayer = new L.OSM.DataLayer(null, {
299 changeset: changesetStyle
303 map._objectLayer.interestingNode = function (node, ways, relations) {
304 if (object.type === "node") {
306 } else if (object.type === "relation") {
307 for (var i = 0; i < relations.length; i++) {
308 if (relations[i].members.indexOf(node) !== -1) return true;
315 map._objectLayer.addData(xml);
316 map._objectLayer.addTo(map);
318 if (callback) callback(map._objectLayer.getBounds());
324 removeObject: function () {
326 if (this._objectLoader) this._objectLoader.abort();
327 if (this._objectLayer) this.removeLayer(this._objectLayer);
330 getState: function () {
332 center: this.getCenter().wrap(),
333 zoom: this.getZoom(),
334 layers: this.getLayersCode()
338 setState: function (state, options) {
339 if (state.center) this.setView(state.center, state.zoom, options);
340 if (state.layers) this.updateLayers(state.layers);
343 setSidebarOverlaid: function (overlaid) {
344 var sidebarWidth = 350;
345 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
346 $("#content").addClass("overlay-sidebar");
347 this.invalidateSize({ pan: false });
348 if ($("html").attr("dir") !== "rtl") {
349 this.panBy([-sidebarWidth, 0], { animate: false });
351 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
352 if ($("html").attr("dir") !== "rtl") {
353 this.panBy([sidebarWidth, 0], { animate: false });
355 $("#content").removeClass("overlay-sidebar");
356 this.invalidateSize({ pan: false });
362 L.Icon.Default.imagePath = "/images/";
364 L.Icon.Default.imageUrls = {
365 "/images/marker-icon.png": OSM.MARKER_ICON,
366 "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
367 "/images/marker-shadow.png": OSM.MARKER_SHADOW
370 L.extend(L.Icon.Default.prototype, {
371 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
373 _getIconUrl: function (name) {
374 var url = this._oldGetIconUrl(name);
375 return L.Icon.Default.imageUrls[url];
379 OSM.getUserIcon = function (url) {
381 iconUrl: url || OSM.MARKER_RED,
383 iconAnchor: [12, 41],
384 popupAnchor: [1, -34],
385 shadowUrl: OSM.MARKER_SHADOW,