1 L.extend(L.LatLngBounds.prototype, {
3 return (this._northEast.lat - this._southWest.lat) *
4 (this._northEast.lng - this._southWest.lng);
8 return new L.LatLngBounds(this._southWest.wrap(), this._northEast.wrap());
12 L.OSM.Map = L.Map.extend({
13 initialize: function (id, options) {
14 L.Map.prototype.initialize.call(this, id, options);
16 var copyright = I18n.t("javascripts.map.copyright", { copyright_url: "/copyright" });
17 var donate = I18n.t("javascripts.map.donate_link_text", { donate_url: "https://donate.openstreetmap.org" });
18 var terms = I18n.t("javascripts.map.terms", { terms_url: "https://wiki.osmfoundation.org/wiki/Terms_of_Use" });
22 this.baseLayers.push(new L.OSM.Mapnik({
23 attribution: copyright + " ♥ " + donate + ". " + terms,
26 name: I18n.t("javascripts.map.base.standard")
29 if (OSM.THUNDERFOREST_KEY) {
30 this.baseLayers.push(new L.OSM.CycleMap({
31 attribution: copyright + ". Tiles courtesy of <a href='https://www.thunderforest.com/' target='_blank'>Andy Allan</a>. " + terms,
32 apikey: OSM.THUNDERFOREST_KEY,
35 name: I18n.t("javascripts.map.base.cycle_map")
38 this.baseLayers.push(new L.OSM.TransportMap({
39 attribution: copyright + ". Tiles courtesy of <a href='https://www.thunderforest.com/' target='_blank'>Andy Allan</a>. " + terms,
40 apikey: OSM.THUNDERFOREST_KEY,
42 keyid: "transportmap",
43 name: I18n.t("javascripts.map.base.transport_map")
47 this.baseLayers.push(new L.OSM.HOT({
48 attribution: copyright + ". Tiles style by <a href='https://www.hotosm.org/' target='_blank'>Humanitarian OpenStreetMap Team</a> hosted by <a href='https://openstreetmap.fr/' target='_blank'>OpenStreetMap France</a>. " + terms,
51 name: I18n.t("javascripts.map.base.hot")
54 this.noteLayer = new L.FeatureGroup();
55 this.noteLayer.options = { code: "N" };
57 this.dataLayer = new L.OSM.DataLayer(null);
58 this.dataLayer.options.code = "D";
60 this.gpsLayer = new L.OSM.GPS({
63 name: I18n.t("javascripts.map.base.gps")
66 this.on("layeradd", function (event) {
67 if (this.baseLayers.indexOf(event.layer) >= 0) {
68 this.setMaxZoom(event.layer.options.maxZoom);
73 updateLayers: function (layerParam) {
74 var layers = layerParam || "M",
77 for (var i = this.baseLayers.length - 1; i >= 0; i--) {
78 if (layers.indexOf(this.baseLayers[i].options.code) >= 0) {
79 this.addLayer(this.baseLayers[i]);
80 layersAdded = layersAdded + this.baseLayers[i].options.code;
81 } else if (i === 0 && layersAdded === "") {
82 this.addLayer(this.baseLayers[i]);
84 this.removeLayer(this.baseLayers[i]);
89 getLayersCode: function () {
91 this.eachLayer(function (layer) {
92 if (layer.options && layer.options.code) {
93 layerConfig += layer.options.code;
99 getMapBaseLayerId: function () {
101 this.eachLayer(function (layer) {
102 if (layer.options && layer.options.keyid) baseLayerId = layer.options.keyid;
107 getUrl: function (marker) {
108 var precision = OSM.zoomPrecision(this.getZoom()),
111 if (marker && this.hasLayer(marker)) {
112 var latLng = marker.getLatLng().wrap();
113 params.mlat = latLng.lat.toFixed(precision);
114 params.mlon = latLng.lng.toFixed(precision);
117 var url = window.location.protocol + "//" + OSM.SERVER_URL + "/",
118 query = qs.stringify(params),
119 hash = OSM.formatHash(this);
121 if (query) url += "?" + query;
122 if (hash) url += hash;
127 getShortUrl: function (marker) {
128 var zoom = this.getZoom(),
129 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
130 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
131 window.location.protocol + "//osm.org/go/" :
132 window.location.protocol + "//" + window.location.hostname + "/go/",
133 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
134 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
135 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
136 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
137 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
138 // and drops the last 4 bits of the full 64 bit Morton code.
139 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
143 for (i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
144 digit = (c1 >> (24 - (6 * i))) & 0x3f;
145 str += char_array.charAt(digit);
147 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
148 digit = (c2 >> (24 - (6 * (i - 5)))) & 0x3f;
149 str += char_array.charAt(digit);
151 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
153 // Called to interlace the bits in x and y, making a Morton code.
154 function interlace(x, y) {
155 var interlaced_x = x,
157 interlaced_x = (interlaced_x | (interlaced_x << 8)) & 0x00ff00ff;
158 interlaced_x = (interlaced_x | (interlaced_x << 4)) & 0x0f0f0f0f;
159 interlaced_x = (interlaced_x | (interlaced_x << 2)) & 0x33333333;
160 interlaced_x = (interlaced_x | (interlaced_x << 1)) & 0x55555555;
161 interlaced_y = (interlaced_y | (interlaced_y << 8)) & 0x00ff00ff;
162 interlaced_y = (interlaced_y | (interlaced_y << 4)) & 0x0f0f0f0f;
163 interlaced_y = (interlaced_y | (interlaced_y << 2)) & 0x33333333;
164 interlaced_y = (interlaced_y | (interlaced_y << 1)) & 0x55555555;
165 return (interlaced_x << 1) | interlaced_y;
169 var layers = this.getLayersCode().replace("M", "");
172 params.layers = layers;
175 if (marker && this.hasLayer(marker)) {
180 params[this._object.type] = this._object.id;
183 var query = qs.stringify(params);
191 getGeoUri: function (marker) {
192 var precision = OSM.zoomPrecision(this.getZoom()),
196 if (marker && this.hasLayer(marker)) {
197 latLng = marker.getLatLng().wrap();
199 latLng = this.getCenter();
202 params.lat = latLng.lat.toFixed(precision);
203 params.lon = latLng.lng.toFixed(precision);
204 params.zoom = this.getZoom();
206 return "geo:" + params.lat + "," + params.lon + "?z=" + params.zoom;
209 addObject: function (object, callback) {
217 var changesetStyle = {
228 this._objectLoader = $.ajax({
229 url: OSM.apiUrl(object),
231 success: function (xml) {
232 map._object = object;
234 map._objectLayer = new L.OSM.DataLayer(null, {
239 changeset: changesetStyle
243 map._objectLayer.interestingNode = function (node, ways, relations) {
244 if (object.type === "node") {
246 } else if (object.type === "relation") {
247 for (var i = 0; i < relations.length; i++) {
248 if (relations[i].members.indexOf(node) !== -1) return true;
255 map._objectLayer.addData(xml);
256 map._objectLayer.addTo(map);
258 if (callback) callback(map._objectLayer.getBounds());
263 removeObject: function () {
265 if (this._objectLoader) this._objectLoader.abort();
266 if (this._objectLayer) this.removeLayer(this._objectLayer);
269 getState: function () {
271 center: this.getCenter().wrap(),
272 zoom: this.getZoom(),
273 layers: this.getLayersCode()
277 setState: function (state, options) {
278 if (state.center) this.setView(state.center, state.zoom, options);
279 if (state.layers) this.updateLayers(state.layers);
282 setSidebarOverlaid: function (overlaid) {
283 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
284 $("#content").addClass("overlay-sidebar");
285 this.invalidateSize({ pan: false })
286 .panBy([-350, 0], { animate: false });
287 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
288 this.panBy([350, 0], { animate: false });
289 $("#content").removeClass("overlay-sidebar");
290 this.invalidateSize({ pan: false });
296 L.Icon.Default.imagePath = "/images/";
298 L.Icon.Default.imageUrls = {
299 "/images/marker-icon.png": OSM.MARKER_ICON,
300 "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
301 "/images/marker-shadow.png": OSM.MARKER_SHADOW
304 L.extend(L.Icon.Default.prototype, {
305 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
307 _getIconUrl: function (name) {
308 var url = this._oldGetIconUrl(name);
309 return L.Icon.Default.imageUrls[url];
313 OSM.getUserIcon = function (url) {
315 iconUrl: url || OSM.MARKER_RED,
317 iconAnchor: [12, 41],
318 popupAnchor: [1, -34],
319 shadowUrl: OSM.MARKER_SHADOW,