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 var copyright = I18n.t("javascripts.map.copyright", { copyright_url: "/copyright" });
19 var donate = I18n.t("javascripts.map.donate_link_text", { donate_url: "https://donate.openstreetmap.org" });
20 var terms = I18n.t("javascripts.map.terms", { terms_url: "https://wiki.osmfoundation.org/wiki/Terms_of_Use" });
21 var cyclosm = I18n.t("javascripts.map.cyclosm", { cyclosm_url: "https://www.cyclosm.org", osmfrance_url: "https://openstreetmap.fr/" });
22 var thunderforest = I18n.t("javascripts.map.thunderforest", { thunderforest_url: "https://www.thunderforest.com/" });
23 var memomaps = I18n.t("javascripts.map.opnvkarte", { memomaps_url: "https://memomaps.de/" });
24 var hotosm = I18n.t("javascripts.map.hotosm", { hotosm_url: "https://www.hotosm.org/", osmfrance_url: "https://openstreetmap.fr/" });
28 this.baseLayers.push(new L.OSM.Mapnik({
29 attribution: copyright + " ♥ " + donate + ". " + terms,
32 name: I18n.t("javascripts.map.base.standard")
35 this.baseLayers.push(new L.OSM.CyclOSM({
36 attribution: copyright + ". " + cyclosm + ". " + terms,
39 name: I18n.t("javascripts.map.base.cyclosm")
42 if (OSM.THUNDERFOREST_KEY) {
43 this.baseLayers.push(new L.OSM.CycleMap({
44 attribution: copyright + ". " + thunderforest + ". " + terms,
45 apikey: OSM.THUNDERFOREST_KEY,
48 name: I18n.t("javascripts.map.base.cycle_map")
51 this.baseLayers.push(new L.OSM.TransportMap({
52 attribution: copyright + ". " + thunderforest + ". " + terms,
53 apikey: OSM.THUNDERFOREST_KEY,
55 keyid: "transportmap",
56 name: I18n.t("javascripts.map.base.transport_map")
60 this.baseLayers.push(new L.OSM.OPNVKarte({
61 attribution: copyright + ". " + memomaps + ". " + terms,
64 name: I18n.t("javascripts.map.base.opnvkarte")
67 this.baseLayers.push(new L.OSM.HOT({
68 attribution: copyright + ". " + hotosm + ". " + terms,
71 name: I18n.t("javascripts.map.base.hot")
74 this.noteLayer = new L.FeatureGroup();
75 this.noteLayer.options = { code: "N" };
77 this.dataLayer = new L.OSM.DataLayer(null);
78 this.dataLayer.options.code = "D";
80 this.gpsLayer = new L.OSM.GPS({
83 name: I18n.t("javascripts.map.base.gps")
86 this.on("layeradd", function (event) {
87 if (this.baseLayers.indexOf(event.layer) >= 0) {
88 this.setMaxZoom(event.layer.options.maxZoom);
93 updateLayers: function (layerParam) {
94 var layers = layerParam || "M",
97 for (var i = this.baseLayers.length - 1; i >= 0; i--) {
98 if (layers.indexOf(this.baseLayers[i].options.code) >= 0) {
99 this.addLayer(this.baseLayers[i]);
100 layersAdded = layersAdded + this.baseLayers[i].options.code;
101 } else if (i === 0 && layersAdded === "") {
102 this.addLayer(this.baseLayers[i]);
104 this.removeLayer(this.baseLayers[i]);
109 getLayersCode: function () {
110 var layerConfig = "";
111 this.eachLayer(function (layer) {
112 if (layer.options && layer.options.code) {
113 layerConfig += layer.options.code;
119 getMapBaseLayerId: function () {
121 this.eachLayer(function (layer) {
122 if (layer.options && layer.options.keyid) baseLayerId = layer.options.keyid;
127 getUrl: function (marker) {
128 var precision = OSM.zoomPrecision(this.getZoom()),
131 if (marker && this.hasLayer(marker)) {
132 var latLng = marker.getLatLng().wrap();
133 params.mlat = latLng.lat.toFixed(precision);
134 params.mlon = latLng.lng.toFixed(precision);
137 var url = window.location.protocol + "//" + OSM.SERVER_URL + "/",
138 query = Qs.stringify(params),
139 hash = OSM.formatHash(this);
141 if (query) url += "?" + query;
142 if (hash) url += hash;
147 getShortUrl: function (marker) {
148 var zoom = this.getZoom(),
149 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
150 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
151 window.location.protocol + "//osm.org/go/" :
152 window.location.protocol + "//" + window.location.hostname + "/go/",
153 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
154 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
155 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
156 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
157 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
158 // and drops the last 4 bits of the full 64 bit Morton code.
159 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
163 for (i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
164 digit = (c1 >> (24 - (6 * i))) & 0x3f;
165 str += char_array.charAt(digit);
167 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
168 digit = (c2 >> (24 - (6 * (i - 5)))) & 0x3f;
169 str += char_array.charAt(digit);
171 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
173 // Called to interlace the bits in x and y, making a Morton code.
174 function interlace(x, y) {
175 var interlaced_x = x,
177 interlaced_x = (interlaced_x | (interlaced_x << 8)) & 0x00ff00ff;
178 interlaced_x = (interlaced_x | (interlaced_x << 4)) & 0x0f0f0f0f;
179 interlaced_x = (interlaced_x | (interlaced_x << 2)) & 0x33333333;
180 interlaced_x = (interlaced_x | (interlaced_x << 1)) & 0x55555555;
181 interlaced_y = (interlaced_y | (interlaced_y << 8)) & 0x00ff00ff;
182 interlaced_y = (interlaced_y | (interlaced_y << 4)) & 0x0f0f0f0f;
183 interlaced_y = (interlaced_y | (interlaced_y << 2)) & 0x33333333;
184 interlaced_y = (interlaced_y | (interlaced_y << 1)) & 0x55555555;
185 return (interlaced_x << 1) | interlaced_y;
189 var layers = this.getLayersCode().replace("M", "");
192 params.layers = layers;
195 if (marker && this.hasLayer(marker)) {
200 params[this._object.type] = this._object.id;
203 var query = Qs.stringify(params);
211 getGeoUri: function (marker) {
212 var precision = OSM.zoomPrecision(this.getZoom()),
216 if (marker && this.hasLayer(marker)) {
217 latLng = marker.getLatLng().wrap();
219 latLng = this.getCenter();
222 params.lat = latLng.lat.toFixed(precision);
223 params.lon = latLng.lng.toFixed(precision);
224 params.zoom = this.getZoom();
226 return "geo:" + params.lat + "," + params.lon + "?z=" + params.zoom;
229 addObject: function (object, callback) {
237 var changesetStyle = {
254 if (object.type === "note") {
255 this._objectLoader = {
256 abort: function () {}
259 this._object = object;
260 this._objectLayer = L.featureGroup().addTo(this);
262 L.circleMarker(object.latLng, haloStyle).addTo(this._objectLayer);
264 L.marker(object.latLng, {
268 }).addTo(this._objectLayer);
270 if (callback) callback(this._objectLayer.getBounds());
271 } else { // element or changeset handled by L.OSM.DataLayer
273 this._objectLoader = $.ajax({
274 url: OSM.apiUrl(object),
276 success: function (xml) {
277 map._object = object;
279 map._objectLayer = new L.OSM.DataLayer(null, {
284 changeset: changesetStyle
288 map._objectLayer.interestingNode = function (node, ways, relations) {
289 if (object.type === "node") {
291 } else if (object.type === "relation") {
292 for (var i = 0; i < relations.length; i++) {
293 if (relations[i].members.indexOf(node) !== -1) return true;
300 map._objectLayer.addData(xml);
301 map._objectLayer.addTo(map);
303 if (callback) callback(map._objectLayer.getBounds());
309 removeObject: function () {
311 if (this._objectLoader) this._objectLoader.abort();
312 if (this._objectLayer) this.removeLayer(this._objectLayer);
315 getState: function () {
317 center: this.getCenter().wrap(),
318 zoom: this.getZoom(),
319 layers: this.getLayersCode()
323 setState: function (state, options) {
324 if (state.center) this.setView(state.center, state.zoom, options);
325 if (state.layers) this.updateLayers(state.layers);
328 setSidebarOverlaid: function (overlaid) {
329 var sidebarWidth = 350;
330 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
331 $("#content").addClass("overlay-sidebar");
332 this.invalidateSize({ pan: false });
333 if ($("html").attr("dir") !== "rtl") {
334 this.panBy([-sidebarWidth, 0], { animate: false });
336 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
337 if ($("html").attr("dir") !== "rtl") {
338 this.panBy([sidebarWidth, 0], { animate: false });
340 $("#content").removeClass("overlay-sidebar");
341 this.invalidateSize({ pan: false });
347 L.Icon.Default.imagePath = "/images/";
349 L.Icon.Default.imageUrls = {
350 "/images/marker-icon.png": OSM.MARKER_ICON,
351 "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
352 "/images/marker-shadow.png": OSM.MARKER_SHADOW
355 L.extend(L.Icon.Default.prototype, {
356 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
358 _getIconUrl: function (name) {
359 var url = this._oldGetIconUrl(name);
360 return L.Icon.Default.imageUrls[url];
364 OSM.getUserIcon = function (url) {
366 iconUrl: url || OSM.MARKER_RED,
368 iconAnchor: [12, 41],
369 popupAnchor: [1, -34],
370 shadowUrl: OSM.MARKER_SHADOW,