1 //= require querystring
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 thunderforest = I18n.t("javascripts.map.thunderforest", { thunderforest_url: "https://www.thunderforest.com/" });
22 var hotosm = I18n.t("javascripts.map.hotosm", { hotosm_url: "https://www.hotosm.org/", osmfrance_url: "https://openstreetmap.fr/" });
26 this.baseLayers.push(new L.OSM.Mapnik({
27 attribution: copyright + " ♥ " + donate + ". " + terms,
30 name: I18n.t("javascripts.map.base.standard")
33 if (OSM.THUNDERFOREST_KEY) {
34 this.baseLayers.push(new L.OSM.CycleMap({
35 attribution: copyright + ". " + thunderforest + ". " + terms,
36 apikey: OSM.THUNDERFOREST_KEY,
39 name: I18n.t("javascripts.map.base.cycle_map")
42 this.baseLayers.push(new L.OSM.TransportMap({
43 attribution: copyright + ". " + thunderforest + ". " + terms,
44 apikey: OSM.THUNDERFOREST_KEY,
46 keyid: "transportmap",
47 name: I18n.t("javascripts.map.base.transport_map")
51 this.baseLayers.push(new L.OSM.HOT({
52 attribution: copyright + ". " + hotosm + ". " + terms,
55 name: I18n.t("javascripts.map.base.hot")
58 this.noteLayer = new L.FeatureGroup();
59 this.noteLayer.options = { code: "N" };
61 this.dataLayer = new L.OSM.DataLayer(null);
62 this.dataLayer.options.code = "D";
64 this.gpsLayer = new L.OSM.GPS({
67 name: I18n.t("javascripts.map.base.gps")
70 this.on("layeradd", function (event) {
71 if (this.baseLayers.indexOf(event.layer) >= 0) {
72 this.setMaxZoom(event.layer.options.maxZoom);
77 updateLayers: function (layerParam) {
78 var layers = layerParam || "M",
81 for (var i = this.baseLayers.length - 1; i >= 0; i--) {
82 if (layers.indexOf(this.baseLayers[i].options.code) >= 0) {
83 this.addLayer(this.baseLayers[i]);
84 layersAdded = layersAdded + this.baseLayers[i].options.code;
85 } else if (i === 0 && layersAdded === "") {
86 this.addLayer(this.baseLayers[i]);
88 this.removeLayer(this.baseLayers[i]);
93 getLayersCode: function () {
95 this.eachLayer(function (layer) {
96 if (layer.options && layer.options.code) {
97 layerConfig += layer.options.code;
103 getMapBaseLayerId: function () {
105 this.eachLayer(function (layer) {
106 if (layer.options && layer.options.keyid) baseLayerId = layer.options.keyid;
111 getUrl: function (marker) {
112 var precision = OSM.zoomPrecision(this.getZoom()),
115 if (marker && this.hasLayer(marker)) {
116 var latLng = marker.getLatLng().wrap();
117 params.mlat = latLng.lat.toFixed(precision);
118 params.mlon = latLng.lng.toFixed(precision);
121 var querystring = require("querystring-component"),
122 url = window.location.protocol + "//" + OSM.SERVER_URL + "/",
123 query = querystring.stringify(params),
124 hash = OSM.formatHash(this);
126 if (query) url += "?" + query;
127 if (hash) url += hash;
132 getShortUrl: function (marker) {
133 var zoom = this.getZoom(),
134 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
135 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
136 window.location.protocol + "//osm.org/go/" :
137 window.location.protocol + "//" + window.location.hostname + "/go/",
138 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
139 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
140 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
141 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
142 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
143 // and drops the last 4 bits of the full 64 bit Morton code.
144 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
148 for (i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
149 digit = (c1 >> (24 - (6 * i))) & 0x3f;
150 str += char_array.charAt(digit);
152 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
153 digit = (c2 >> (24 - (6 * (i - 5)))) & 0x3f;
154 str += char_array.charAt(digit);
156 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
158 // Called to interlace the bits in x and y, making a Morton code.
159 function interlace(x, y) {
160 var interlaced_x = x,
162 interlaced_x = (interlaced_x | (interlaced_x << 8)) & 0x00ff00ff;
163 interlaced_x = (interlaced_x | (interlaced_x << 4)) & 0x0f0f0f0f;
164 interlaced_x = (interlaced_x | (interlaced_x << 2)) & 0x33333333;
165 interlaced_x = (interlaced_x | (interlaced_x << 1)) & 0x55555555;
166 interlaced_y = (interlaced_y | (interlaced_y << 8)) & 0x00ff00ff;
167 interlaced_y = (interlaced_y | (interlaced_y << 4)) & 0x0f0f0f0f;
168 interlaced_y = (interlaced_y | (interlaced_y << 2)) & 0x33333333;
169 interlaced_y = (interlaced_y | (interlaced_y << 1)) & 0x55555555;
170 return (interlaced_x << 1) | interlaced_y;
174 var layers = this.getLayersCode().replace("M", "");
177 params.layers = layers;
180 if (marker && this.hasLayer(marker)) {
185 params[this._object.type] = this._object.id;
188 var querystring = require("querystring-component"),
189 query = querystring.stringify(params);
197 getGeoUri: function (marker) {
198 var precision = OSM.zoomPrecision(this.getZoom()),
202 if (marker && this.hasLayer(marker)) {
203 latLng = marker.getLatLng().wrap();
205 latLng = this.getCenter();
208 params.lat = latLng.lat.toFixed(precision);
209 params.lon = latLng.lng.toFixed(precision);
210 params.zoom = this.getZoom();
212 return "geo:" + params.lat + "," + params.lon + "?z=" + params.zoom;
215 addObject: function (object, callback) {
223 var changesetStyle = {
234 this._objectLoader = $.ajax({
235 url: OSM.apiUrl(object),
237 success: function (xml) {
238 map._object = object;
240 map._objectLayer = new L.OSM.DataLayer(null, {
245 changeset: changesetStyle
249 map._objectLayer.interestingNode = function (node, ways, relations) {
250 if (object.type === "node") {
252 } else if (object.type === "relation") {
253 for (var i = 0; i < relations.length; i++) {
254 if (relations[i].members.indexOf(node) !== -1) return true;
261 map._objectLayer.addData(xml);
262 map._objectLayer.addTo(map);
264 if (callback) callback(map._objectLayer.getBounds());
269 removeObject: function () {
271 if (this._objectLoader) this._objectLoader.abort();
272 if (this._objectLayer) this.removeLayer(this._objectLayer);
275 getState: function () {
277 center: this.getCenter().wrap(),
278 zoom: this.getZoom(),
279 layers: this.getLayersCode()
283 setState: function (state, options) {
284 if (state.center) this.setView(state.center, state.zoom, options);
285 if (state.layers) this.updateLayers(state.layers);
288 setSidebarOverlaid: function (overlaid) {
289 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
290 $("#content").addClass("overlay-sidebar");
291 this.invalidateSize({ pan: false })
292 .panBy([-350, 0], { animate: false });
293 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
294 this.panBy([350, 0], { animate: false });
295 $("#content").removeClass("overlay-sidebar");
296 this.invalidateSize({ pan: false });
302 L.Icon.Default.imagePath = "/images/";
304 L.Icon.Default.imageUrls = {
305 "/images/marker-icon.png": OSM.MARKER_ICON,
306 "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
307 "/images/marker-shadow.png": OSM.MARKER_SHADOW
310 L.extend(L.Icon.Default.prototype, {
311 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
313 _getIconUrl: function (name) {
314 var url = this._oldGetIconUrl(name);
315 return L.Icon.Default.imageUrls[url];
319 OSM.getUserIcon = function (url) {
321 iconUrl: url || OSM.MARKER_RED,
323 iconAnchor: [12, 41],
324 popupAnchor: [1, -34],
325 shadowUrl: OSM.MARKER_SHADOW,