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" });
21 this.baseLayers.push(new L.OSM.Mapnik({
22 attribution: copyright + " ♥ " + donate,
25 name: I18n.t("javascripts.map.base.standard")
28 if (OSM.THUNDERFOREST_KEY) {
29 this.baseLayers.push(new L.OSM.CycleMap({
30 attribution: copyright + ". Tiles courtesy of <a href='https://www.thunderforest.com/' target='_blank'>Andy Allan</a>",
31 apikey: OSM.THUNDERFOREST_KEY,
34 name: I18n.t("javascripts.map.base.cycle_map")
37 this.baseLayers.push(new L.OSM.TransportMap({
38 attribution: copyright + ". Tiles courtesy of <a href='https://www.thunderforest.com/' target='_blank'>Andy Allan</a>",
39 apikey: OSM.THUNDERFOREST_KEY,
41 keyid: "transportmap",
42 name: I18n.t("javascripts.map.base.transport_map")
46 this.baseLayers.push(new L.OSM.HOT({
47 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>",
50 name: I18n.t("javascripts.map.base.hot")
53 this.noteLayer = new L.FeatureGroup();
54 this.noteLayer.options = { code: "N" };
56 this.dataLayer = new L.OSM.DataLayer(null);
57 this.dataLayer.options.code = "D";
59 this.gpsLayer = new L.OSM.GPS({
62 name: I18n.t("javascripts.map.base.gps")
65 this.on("layeradd", function (event) {
66 if (this.baseLayers.indexOf(event.layer) >= 0) {
67 this.setMaxZoom(event.layer.options.maxZoom);
72 updateLayers: function (layerParam) {
73 layerParam = layerParam || "M";
76 for (var i = this.baseLayers.length - 1; i >= 0; i--) {
77 if (layerParam.indexOf(this.baseLayers[i].options.code) >= 0) {
78 this.addLayer(this.baseLayers[i]);
79 layersAdded = layersAdded + this.baseLayers[i].options.code;
80 } else if (i === 0 && layersAdded === "") {
81 this.addLayer(this.baseLayers[i]);
83 this.removeLayer(this.baseLayers[i]);
88 getLayersCode: function () {
90 this.eachLayer(function (layer) {
91 if (layer.options && layer.options.code) {
92 layerConfig += layer.options.code;
98 getMapBaseLayerId: function () {
100 this.eachLayer(function (layer) {
101 if (layer.options && layer.options.keyid) baseLayerId = layer.options.keyid;
106 getUrl: function (marker) {
107 var precision = OSM.zoomPrecision(this.getZoom()),
110 if (marker && this.hasLayer(marker)) {
111 var latLng = marker.getLatLng().wrap();
112 params.mlat = latLng.lat.toFixed(precision);
113 params.mlon = latLng.lng.toFixed(precision);
116 var url = window.location.protocol + "//" + OSM.SERVER_URL + "/",
117 query = querystring.stringify(params),
118 hash = OSM.formatHash(this);
120 if (query) url += "?" + query;
121 if (hash) url += hash;
126 getShortUrl: function (marker) {
127 var zoom = this.getZoom(),
128 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
129 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
130 window.location.protocol + "//osm.org/go/" :
131 window.location.protocol + "//" + window.location.hostname + "/go/",
132 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
133 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
134 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
135 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
136 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
137 // and drops the last 4 bits of the full 64 bit Morton code.
138 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
142 for (i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
143 digit = (c1 >> (24 - (6 * i))) & 0x3f;
144 str += char_array.charAt(digit);
146 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
147 digit = (c2 >> (24 - (6 * (i - 5)))) & 0x3f;
148 str += char_array.charAt(digit);
150 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
152 // Called to interlace the bits in x and y, making a Morton code.
153 function interlace(x, y) {
154 x = (x | (x << 8)) & 0x00ff00ff;
155 x = (x | (x << 4)) & 0x0f0f0f0f;
156 x = (x | (x << 2)) & 0x33333333;
157 x = (x | (x << 1)) & 0x55555555;
158 y = (y | (y << 8)) & 0x00ff00ff;
159 y = (y | (y << 4)) & 0x0f0f0f0f;
160 y = (y | (y << 2)) & 0x33333333;
161 y = (y | (y << 1)) & 0x55555555;
166 var layers = this.getLayersCode().replace("M", "");
169 params.layers = layers;
172 if (marker && this.hasLayer(marker)) {
177 params[this._object.type] = this._object.id;
180 var query = querystring.stringify(params);
188 getGeoUri: function (marker) {
189 var precision = OSM.zoomPrecision(this.getZoom()),
193 if (marker && this.hasLayer(marker)) {
194 latLng = marker.getLatLng().wrap();
196 latLng = this.getCenter();
199 params.lat = latLng.lat.toFixed(precision);
200 params.lon = latLng.lng.toFixed(precision);
201 params.zoom = this.getZoom();
203 return "geo:" + params.lat + "," + params.lon + "?z=" + params.zoom;
206 addObject: function (object, callback) {
214 var changesetStyle = {
225 this._objectLoader = $.ajax({
226 url: OSM.apiUrl(object),
228 success: function (xml) {
229 map._object = object;
231 map._objectLayer = new L.OSM.DataLayer(null, {
236 changeset: changesetStyle
240 map._objectLayer.interestingNode = function (node, ways, relations) {
241 if (object.type === "node") {
243 } else if (object.type === "relation") {
244 for (var i = 0; i < relations.length; i++) {
245 if (relations[i].members.indexOf(node) !== -1) return true;
252 map._objectLayer.addData(xml);
253 map._objectLayer.addTo(map);
255 if (callback) callback(map._objectLayer.getBounds());
260 removeObject: function () {
262 if (this._objectLoader) this._objectLoader.abort();
263 if (this._objectLayer) this.removeLayer(this._objectLayer);
266 getState: function () {
268 center: this.getCenter().wrap(),
269 zoom: this.getZoom(),
270 layers: this.getLayersCode()
274 setState: function (state, options) {
275 if (state.center) this.setView(state.center, state.zoom, options);
276 if (state.layers) this.updateLayers(state.layers);
279 setSidebarOverlaid: function (overlaid) {
280 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
281 $("#content").addClass("overlay-sidebar");
282 this.invalidateSize({ pan: false })
283 .panBy([-350, 0], { animate: false });
284 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
285 this.panBy([350, 0], { animate: false });
286 $("#content").removeClass("overlay-sidebar");
287 this.invalidateSize({ pan: false });
293 L.Icon.Default.imagePath = "/images/";
295 L.Icon.Default.imageUrls = {
296 "/images/marker-icon.png": OSM.MARKER_ICON,
297 "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
298 "/images/marker-shadow.png": OSM.MARKER_SHADOW
301 L.extend(L.Icon.Default.prototype, {
302 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
304 _getIconUrl: function (name) {
305 var url = this._oldGetIconUrl(name);
306 return L.Icon.Default.imageUrls[url];
310 OSM.getUserIcon = function (url) {
312 iconUrl: url || OSM.MARKER_RED,
314 iconAnchor: [12, 41],
315 popupAnchor: [1, -34],
316 shadowUrl: OSM.MARKER_SHADOW,