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 tracestrack_link = $("<a>", {
55 href: "https://www.tracestrack.com/",
57 text: I18n.t("javascripts.map.tracestrack")
59 var tracestrack = I18n.t("javascripts.map.tracestrack_credit", { tracestrack_link: tracestrack_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 if (OSM.TRACESTRACK_KEY) {
103 this.baseLayers.push(new L.OSM.TracestrackTopo({
104 attribution: copyright + ". " + tracestrack + ". " + terms,
105 apikey: OSM.TRACESTRACK_KEY,
107 keyid: "tracestracktopo",
108 name: I18n.t("javascripts.map.base.tracestracktop_topo")
112 this.baseLayers.push(new L.OSM.HOT({
113 attribution: copyright + ". " + hotosm + ". " + terms,
116 name: I18n.t("javascripts.map.base.hot")
119 this.noteLayer = new L.FeatureGroup();
120 this.noteLayer.options = { code: "N" };
122 this.dataLayer = new L.OSM.DataLayer(null);
123 this.dataLayer.options.code = "D";
125 this.gpsLayer = new L.OSM.GPS({
130 this.on("layeradd", function (event) {
131 if (this.baseLayers.indexOf(event.layer) >= 0) {
132 this.setMaxZoom(event.layer.options.maxZoom);
137 updateLayers: function (layerParam) {
138 var layers = layerParam || "M",
141 for (var i = this.baseLayers.length - 1; i >= 0; i--) {
142 if (layers.indexOf(this.baseLayers[i].options.code) >= 0) {
143 this.addLayer(this.baseLayers[i]);
144 layersAdded = layersAdded + this.baseLayers[i].options.code;
145 } else if (i === 0 && layersAdded === "") {
146 this.addLayer(this.baseLayers[i]);
148 this.removeLayer(this.baseLayers[i]);
153 getLayersCode: function () {
154 var layerConfig = "";
155 this.eachLayer(function (layer) {
156 if (layer.options && layer.options.code) {
157 layerConfig += layer.options.code;
163 getMapBaseLayerId: function () {
165 this.eachLayer(function (layer) {
166 if (layer.options && layer.options.keyid) baseLayerId = layer.options.keyid;
171 getUrl: function (marker) {
172 var precision = OSM.zoomPrecision(this.getZoom()),
175 if (marker && this.hasLayer(marker)) {
176 var latLng = marker.getLatLng().wrap();
177 params.mlat = latLng.lat.toFixed(precision);
178 params.mlon = latLng.lng.toFixed(precision);
181 var url = window.location.protocol + "//" + OSM.SERVER_URL + "/",
182 query = Qs.stringify(params),
183 hash = OSM.formatHash(this);
185 if (query) url += "?" + query;
186 if (hash) url += hash;
191 getShortUrl: function (marker) {
192 var zoom = this.getZoom(),
193 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
194 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
195 window.location.protocol + "//osm.org/go/" :
196 window.location.protocol + "//" + window.location.hostname + "/go/",
197 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
198 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
199 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
200 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
201 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
202 // and drops the last 4 bits of the full 64 bit Morton code.
203 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
207 for (i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
208 digit = (c1 >> (24 - (6 * i))) & 0x3f;
209 str += char_array.charAt(digit);
211 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
212 digit = (c2 >> (24 - (6 * (i - 5)))) & 0x3f;
213 str += char_array.charAt(digit);
215 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
217 // Called to interlace the bits in x and y, making a Morton code.
218 function interlace(x, y) {
219 var interlaced_x = x,
221 interlaced_x = (interlaced_x | (interlaced_x << 8)) & 0x00ff00ff;
222 interlaced_x = (interlaced_x | (interlaced_x << 4)) & 0x0f0f0f0f;
223 interlaced_x = (interlaced_x | (interlaced_x << 2)) & 0x33333333;
224 interlaced_x = (interlaced_x | (interlaced_x << 1)) & 0x55555555;
225 interlaced_y = (interlaced_y | (interlaced_y << 8)) & 0x00ff00ff;
226 interlaced_y = (interlaced_y | (interlaced_y << 4)) & 0x0f0f0f0f;
227 interlaced_y = (interlaced_y | (interlaced_y << 2)) & 0x33333333;
228 interlaced_y = (interlaced_y | (interlaced_y << 1)) & 0x55555555;
229 return (interlaced_x << 1) | interlaced_y;
233 var layers = this.getLayersCode().replace("M", "");
236 params.layers = layers;
239 if (marker && this.hasLayer(marker)) {
244 params[this._object.type] = this._object.id;
247 var query = Qs.stringify(params);
255 getGeoUri: function (marker) {
256 var precision = OSM.zoomPrecision(this.getZoom()),
260 if (marker && this.hasLayer(marker)) {
261 latLng = marker.getLatLng().wrap();
263 latLng = this.getCenter();
266 params.lat = latLng.lat.toFixed(precision);
267 params.lon = latLng.lng.toFixed(precision);
268 params.zoom = this.getZoom();
270 return "geo:" + params.lat + "," + params.lon + "?z=" + params.zoom;
273 addObject: function (object, callback) {
281 var changesetStyle = {
298 if (object.type === "note") {
299 this._objectLoader = {
300 abort: function () {}
303 this._object = object;
304 this._objectLayer = L.featureGroup().addTo(this);
306 L.circleMarker(object.latLng, haloStyle).addTo(this._objectLayer);
309 L.marker(object.latLng, {
313 }).addTo(this._objectLayer);
316 if (callback) callback(this._objectLayer.getBounds());
317 } else { // element or changeset handled by L.OSM.DataLayer
319 this._objectLoader = $.ajax({
320 url: OSM.apiUrl(object),
322 success: function (xml) {
323 map._object = object;
325 map._objectLayer = new L.OSM.DataLayer(null, {
330 changeset: changesetStyle
334 map._objectLayer.interestingNode = function (node, ways, relations) {
335 if (object.type === "node") {
337 } else if (object.type === "relation") {
338 for (var i = 0; i < relations.length; i++) {
339 if (relations[i].members.indexOf(node) !== -1) return true;
346 map._objectLayer.addData(xml);
347 map._objectLayer.addTo(map);
349 if (callback) callback(map._objectLayer.getBounds());
355 removeObject: function () {
357 if (this._objectLoader) this._objectLoader.abort();
358 if (this._objectLayer) this.removeLayer(this._objectLayer);
361 getState: function () {
363 center: this.getCenter().wrap(),
364 zoom: this.getZoom(),
365 layers: this.getLayersCode()
369 setState: function (state, options) {
370 if (state.center) this.setView(state.center, state.zoom, options);
371 if (state.layers) this.updateLayers(state.layers);
374 setSidebarOverlaid: function (overlaid) {
375 var sidebarWidth = 350;
376 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
377 $("#content").addClass("overlay-sidebar");
378 this.invalidateSize({ pan: false });
379 if ($("html").attr("dir") !== "rtl") {
380 this.panBy([-sidebarWidth, 0], { animate: false });
382 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
383 if ($("html").attr("dir") !== "rtl") {
384 this.panBy([sidebarWidth, 0], { animate: false });
386 $("#content").removeClass("overlay-sidebar");
387 this.invalidateSize({ pan: false });
393 L.Icon.Default.imagePath = "/images/";
395 L.Icon.Default.imageUrls = {
396 "/images/marker-icon.png": OSM.MARKER_ICON,
397 "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
398 "/images/marker-shadow.png": OSM.MARKER_SHADOW
401 L.extend(L.Icon.Default.prototype, {
402 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
404 _getIconUrl: function (name) {
405 var url = this._oldGetIconUrl(name);
406 return L.Icon.Default.imageUrls[url];
410 OSM.getUserIcon = function (url) {
412 iconUrl: url || OSM.MARKER_RED,
414 iconAnchor: [12, 41],
415 popupAnchor: [1, -34],
416 shadowUrl: OSM.MARKER_SHADOW,