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_link = $("<a>", {
20 text: I18n.t("javascripts.map.openstreetmap_contributors")
22 var copyright = I18n.t("javascripts.map.copyright_text", { copyright_link: copyright_link });
24 var donate = $("<a>", {
25 "href": "https://supporting.openstreetmap.org",
26 "class": "donate-attr",
27 "text": I18n.t("javascripts.map.make_a_donation")
30 var terms = $("<a>", {
31 href: "https://wiki.osmfoundation.org/wiki/Terms_of_Use",
32 text: I18n.t("javascripts.map.website_and_api_terms")
35 var cyclosm_link = $("<a>", {
36 href: "https://www.cyclosm.org",
38 text: I18n.t("javascripts.map.cyclosm_name")
40 var osm_france_link = $("<a>", {
41 href: "https://openstreetmap.fr/",
43 text: I18n.t("javascripts.map.osm_france")
45 var cyclosm = I18n.t("javascripts.map.cyclosm_credit", { cyclosm_link: cyclosm_link, osm_france_link: osm_france_link });
47 var thunderforest_link = $("<a>", {
48 href: "https://www.thunderforest.com/",
50 text: I18n.t("javascripts.map.andy_allan")
52 var thunderforest = I18n.t("javascripts.map.thunderforest_credit", { thunderforest_link: thunderforest_link });
54 var memomaps_link = $("<a>", {
55 href: "https://memomaps.de/",
57 text: I18n.t("javascripts.map.memomaps")
59 var memomaps = I18n.t("javascripts.map.opnvkarte_credit", { memomaps_link: memomaps_link });
61 var hotosm_link = $("<a>", {
62 href: "https://www.hotosm.org/",
64 text: I18n.t("javascripts.map.hotosm_name")
66 var hotosm = I18n.t("javascripts.map.hotosm_credit", { hotosm_link: hotosm_link, osm_france_link: osm_france_link });
70 this.baseLayers.push(new L.OSM.Mapnik({
71 attribution: copyright + " ♥ " + donate + ". " + terms,
74 name: I18n.t("javascripts.map.base.standard")
77 this.baseLayers.push(new L.OSM.CyclOSM({
78 attribution: copyright + ". " + cyclosm + ". " + terms,
81 name: I18n.t("javascripts.map.base.cyclosm")
84 if (OSM.THUNDERFOREST_KEY) {
85 this.baseLayers.push(new L.OSM.CycleMap({
86 attribution: copyright + ". " + thunderforest + ". " + terms,
87 apikey: OSM.THUNDERFOREST_KEY,
90 name: I18n.t("javascripts.map.base.cycle_map")
93 this.baseLayers.push(new L.OSM.TransportMap({
94 attribution: copyright + ". " + thunderforest + ". " + terms,
95 apikey: OSM.THUNDERFOREST_KEY,
97 keyid: "transportmap",
98 name: I18n.t("javascripts.map.base.transport_map")
102 this.baseLayers.push(new L.OSM.OPNVKarte({
103 attribution: copyright + ". " + memomaps + ". " + terms,
106 name: I18n.t("javascripts.map.base.opnvkarte")
109 this.baseLayers.push(new L.OSM.HOT({
110 attribution: copyright + ". " + hotosm + ". " + terms,
113 name: I18n.t("javascripts.map.base.hot")
116 this.noteLayer = new L.FeatureGroup();
117 this.noteLayer.options = { code: "N" };
119 this.dataLayer = new L.OSM.DataLayer(null);
120 this.dataLayer.options.code = "D";
122 this.gpsLayer = new L.OSM.GPS({
125 name: I18n.t("javascripts.map.base.gps")
128 this.on("layeradd", function (event) {
129 if (this.baseLayers.indexOf(event.layer) >= 0) {
130 this.setMaxZoom(event.layer.options.maxZoom);
135 updateLayers: function (layerParam) {
136 var layers = layerParam || "M",
139 for (var i = this.baseLayers.length - 1; i >= 0; i--) {
140 if (layers.indexOf(this.baseLayers[i].options.code) >= 0) {
141 this.addLayer(this.baseLayers[i]);
142 layersAdded = layersAdded + this.baseLayers[i].options.code;
143 } else if (i === 0 && layersAdded === "") {
144 this.addLayer(this.baseLayers[i]);
146 this.removeLayer(this.baseLayers[i]);
151 getLayersCode: function () {
152 var layerConfig = "";
153 this.eachLayer(function (layer) {
154 if (layer.options && layer.options.code) {
155 layerConfig += layer.options.code;
161 getMapBaseLayerId: function () {
163 this.eachLayer(function (layer) {
164 if (layer.options && layer.options.keyid) baseLayerId = layer.options.keyid;
169 getUrl: function (marker) {
170 var precision = OSM.zoomPrecision(this.getZoom()),
173 if (marker && this.hasLayer(marker)) {
174 var latLng = marker.getLatLng().wrap();
175 params.mlat = latLng.lat.toFixed(precision);
176 params.mlon = latLng.lng.toFixed(precision);
179 var url = window.location.protocol + "//" + OSM.SERVER_URL + "/",
180 query = Qs.stringify(params),
181 hash = OSM.formatHash(this);
183 if (query) url += "?" + query;
184 if (hash) url += hash;
189 getShortUrl: function (marker) {
190 var zoom = this.getZoom(),
191 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
192 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
193 window.location.protocol + "//osm.org/go/" :
194 window.location.protocol + "//" + window.location.hostname + "/go/",
195 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
196 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
197 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
198 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
199 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
200 // and drops the last 4 bits of the full 64 bit Morton code.
201 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
205 for (i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
206 digit = (c1 >> (24 - (6 * i))) & 0x3f;
207 str += char_array.charAt(digit);
209 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
210 digit = (c2 >> (24 - (6 * (i - 5)))) & 0x3f;
211 str += char_array.charAt(digit);
213 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
215 // Called to interlace the bits in x and y, making a Morton code.
216 function interlace(x, y) {
217 var interlaced_x = x,
219 interlaced_x = (interlaced_x | (interlaced_x << 8)) & 0x00ff00ff;
220 interlaced_x = (interlaced_x | (interlaced_x << 4)) & 0x0f0f0f0f;
221 interlaced_x = (interlaced_x | (interlaced_x << 2)) & 0x33333333;
222 interlaced_x = (interlaced_x | (interlaced_x << 1)) & 0x55555555;
223 interlaced_y = (interlaced_y | (interlaced_y << 8)) & 0x00ff00ff;
224 interlaced_y = (interlaced_y | (interlaced_y << 4)) & 0x0f0f0f0f;
225 interlaced_y = (interlaced_y | (interlaced_y << 2)) & 0x33333333;
226 interlaced_y = (interlaced_y | (interlaced_y << 1)) & 0x55555555;
227 return (interlaced_x << 1) | interlaced_y;
231 var layers = this.getLayersCode().replace("M", "");
234 params.layers = layers;
237 if (marker && this.hasLayer(marker)) {
242 params[this._object.type] = this._object.id;
245 var query = Qs.stringify(params);
253 getGeoUri: function (marker) {
254 var precision = OSM.zoomPrecision(this.getZoom()),
258 if (marker && this.hasLayer(marker)) {
259 latLng = marker.getLatLng().wrap();
261 latLng = this.getCenter();
264 params.lat = latLng.lat.toFixed(precision);
265 params.lon = latLng.lng.toFixed(precision);
266 params.zoom = this.getZoom();
268 return "geo:" + params.lat + "," + params.lon + "?z=" + params.zoom;
271 addObject: function (object, callback) {
279 var changesetStyle = {
296 if (object.type === "note") {
297 this._objectLoader = {
298 abort: function () {}
301 this._object = object;
302 this._objectLayer = L.featureGroup().addTo(this);
304 L.circleMarker(object.latLng, haloStyle).addTo(this._objectLayer);
307 L.marker(object.latLng, {
311 }).addTo(this._objectLayer);
314 if (callback) callback(this._objectLayer.getBounds());
315 } else { // element or changeset handled by L.OSM.DataLayer
317 this._objectLoader = $.ajax({
318 url: OSM.apiUrl(object),
320 success: function (xml) {
321 map._object = object;
323 map._objectLayer = new L.OSM.DataLayer(null, {
328 changeset: changesetStyle
332 map._objectLayer.interestingNode = function (node, ways, relations) {
333 if (object.type === "node") {
335 } else if (object.type === "relation") {
336 for (var i = 0; i < relations.length; i++) {
337 if (relations[i].members.indexOf(node) !== -1) return true;
344 map._objectLayer.addData(xml);
345 map._objectLayer.addTo(map);
347 if (callback) callback(map._objectLayer.getBounds());
353 removeObject: function () {
355 if (this._objectLoader) this._objectLoader.abort();
356 if (this._objectLayer) this.removeLayer(this._objectLayer);
359 getState: function () {
361 center: this.getCenter().wrap(),
362 zoom: this.getZoom(),
363 layers: this.getLayersCode()
367 setState: function (state, options) {
368 if (state.center) this.setView(state.center, state.zoom, options);
369 if (state.layers) this.updateLayers(state.layers);
372 setSidebarOverlaid: function (overlaid) {
373 var sidebarWidth = 350;
374 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
375 $("#content").addClass("overlay-sidebar");
376 this.invalidateSize({ pan: false });
377 if ($("html").attr("dir") !== "rtl") {
378 this.panBy([-sidebarWidth, 0], { animate: false });
380 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
381 if ($("html").attr("dir") !== "rtl") {
382 this.panBy([sidebarWidth, 0], { animate: false });
384 $("#content").removeClass("overlay-sidebar");
385 this.invalidateSize({ pan: false });
391 L.Icon.Default.imagePath = "/images/";
393 L.Icon.Default.imageUrls = {
394 "/images/marker-icon.png": OSM.MARKER_ICON,
395 "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
396 "/images/marker-shadow.png": OSM.MARKER_SHADOW
399 L.extend(L.Icon.Default.prototype, {
400 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
402 _getIconUrl: function (name) {
403 var url = this._oldGetIconUrl(name);
404 return L.Icon.Default.imageUrls[url];
408 OSM.getUserIcon = function (url) {
410 iconUrl: url || OSM.MARKER_RED,
412 iconAnchor: [12, 41],
413 popupAnchor: [1, -34],
414 shadowUrl: OSM.MARKER_SHADOW,