]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/leaflet.map.js
Add thunderforest API key to example configuration
[rails.git] / app / assets / javascripts / leaflet.map.js
1 L.extend(L.LatLngBounds.prototype, {
2   getSize: function () {
3     return (this._northEast.lat - this._southWest.lat) *
4            (this._northEast.lng - this._southWest.lng);
5   },
6
7   wrap: function () {
8     return new L.LatLngBounds(this._southWest.wrap(), this._northEast.wrap());
9   }
10 });
11
12 L.OSM.Map = L.Map.extend({
13   initialize: function(id, options) {
14     L.Map.prototype.initialize.call(this, id, options);
15
16     var copyright = I18n.t('javascripts.map.copyright', {copyright_url: '/copyright'});
17     var donate = I18n.t('javascripts.map.donate_link_text', {donate_url: 'http://donate.openstreetmap.org'});
18
19     this.baseLayers = [];
20
21     this.baseLayers.push(new L.OSM.Mapnik({
22       attribution: copyright + " ♥ " + donate,
23       code: "M",
24       keyid: "mapnik",
25       name: I18n.t("javascripts.map.base.standard")
26     }));
27
28     if (OSM.THUNDERFOREST_KEY) {
29       this.baseLayers.push(new L.OSM.CycleMap({
30         attribution: copyright + ". Tiles courtesy of <a href='http://www.thunderforest.com/' target='_blank'>Andy Allan</a>",
31         apikey: OSM.THUNDERFOREST_KEY,
32         code: "C",
33         keyid: "cyclemap",
34         name: I18n.t("javascripts.map.base.cycle_map")
35       }));
36
37       this.baseLayers.push(new L.OSM.TransportMap({
38         attribution: copyright + ". Tiles courtesy of <a href='http://www.thunderforest.com/' target='_blank'>Andy Allan</a>",
39         apikey: OSM.THUNDERFOREST_KEY,
40         code: "T",
41         keyid: "transportmap",
42         name: I18n.t("javascripts.map.base.transport_map")
43       }));
44     }
45
46     this.baseLayers.push(new L.OSM.HOT({
47       attribution: copyright + ". Tiles courtesy of <a href='http://hot.openstreetmap.org/' target='_blank'>Humanitarian OpenStreetMap Team</a>",
48       code: "H",
49       keyid: "hot",
50       name: I18n.t("javascripts.map.base.hot")
51     }));
52
53     this.noteLayer = new L.FeatureGroup();
54     this.noteLayer.options = {code: 'N'};
55
56     this.dataLayer = new L.OSM.DataLayer(null);
57     this.dataLayer.options.code = 'D';
58   },
59
60   updateLayers: function(layerParam) {
61     layerParam = layerParam || "M";
62     var layersAdded = "";
63
64     for (var i = this.baseLayers.length - 1; i >= 0; i--) {
65       if (layerParam.indexOf(this.baseLayers[i].options.code) >= 0) {
66         this.addLayer(this.baseLayers[i]);
67         layersAdded = layersAdded + this.baseLayers[i].options.code;
68       } else if (i === 0 && layersAdded === "") {
69         this.addLayer(this.baseLayers[i]);
70       } else {
71         this.removeLayer(this.baseLayers[i]);
72       }
73     }
74   },
75
76   getLayersCode: function () {
77     var layerConfig = '';
78     for (var i in this._layers) { // TODO: map.eachLayer
79       var layer = this._layers[i];
80       if (layer.options && layer.options.code) {
81         layerConfig += layer.options.code;
82       }
83     }
84     return layerConfig;
85   },
86
87   getMapBaseLayerId: function () {
88     for (var i in this._layers) { // TODO: map.eachLayer
89       var layer = this._layers[i];
90       if (layer.options && layer.options.keyid) return layer.options.keyid;
91     }
92   },
93
94   getUrl: function(marker) {
95     var precision = OSM.zoomPrecision(this.getZoom()),
96         params = {};
97
98     if (marker && this.hasLayer(marker)) {
99       var latLng = marker.getLatLng().wrap();
100       params.mlat = latLng.lat.toFixed(precision);
101       params.mlon = latLng.lng.toFixed(precision);
102     }
103
104     var url = 'http://' + OSM.SERVER_URL + '/',
105       query = querystring.stringify(params),
106       hash = OSM.formatHash(this);
107
108     if (query) url += '?' + query;
109     if (hash) url += hash;
110
111     return url;
112   },
113
114   getShortUrl: function(marker) {
115     var zoom = this.getZoom(),
116       latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
117       str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
118         'http://osm.org/go/' : 'http://' + window.location.hostname + '/go/',
119       char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
120       x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
121       y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
122       // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
123       // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
124       // and drops the last 4 bits of the full 64 bit Morton code.
125       c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
126       digit;
127
128     for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
129       digit = (c1 >> (24 - 6 * i)) & 0x3f;
130       str += char_array.charAt(digit);
131     }
132     for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
133       digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
134       str += char_array.charAt(digit);
135     }
136     for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
137
138     // Called to interlace the bits in x and y, making a Morton code.
139     function interlace(x, y) {
140       x = (x | (x << 8)) & 0x00ff00ff;
141       x = (x | (x << 4)) & 0x0f0f0f0f;
142       x = (x | (x << 2)) & 0x33333333;
143       x = (x | (x << 1)) & 0x55555555;
144       y = (y | (y << 8)) & 0x00ff00ff;
145       y = (y | (y << 4)) & 0x0f0f0f0f;
146       y = (y | (y << 2)) & 0x33333333;
147       y = (y | (y << 1)) & 0x55555555;
148       return (x << 1) | y;
149     }
150
151     var params = {};
152     var layers = this.getLayersCode().replace('M', '');
153
154     if (layers) {
155       params.layers = layers;
156     }
157
158     if (marker && this.hasLayer(marker)) {
159       params.m = '';
160     }
161
162     if (this._object) {
163       params[this._object.type] = this._object.id;
164     }
165
166     var query = querystring.stringify(params);
167     if (query) {
168       str += '?' + query;
169     }
170
171     return str;
172   },
173
174   getGeoUri: function(marker) {
175     var precision = OSM.zoomPrecision(this.getZoom()),
176         latLng,
177         params = {};
178
179     if (marker && this.hasLayer(marker)) {
180       latLng = marker.getLatLng().wrap();
181     } else {
182       latLng = this.getCenter();
183     }
184
185     params.lat = latLng.lat.toFixed(precision);
186     params.lon = latLng.lng.toFixed(precision);
187     params.zoom = this.getZoom();
188
189     return 'geo:' + params.lat + ',' + params.lon + '?z=' + params.zoom;
190   },
191
192   addObject: function(object, callback) {
193     var objectStyle = {
194       color: "#FF6200",
195       weight: 4,
196       opacity: 1,
197       fillOpacity: 0.5
198     };
199
200     var changesetStyle = {
201       weight: 4,
202       color: '#FF9500',
203       opacity: 1,
204       fillOpacity: 0,
205       clickable: false
206     };
207
208     this._object = object;
209
210     if (this._objectLoader) this._objectLoader.abort();
211     if (this._objectLayer) this.removeLayer(this._objectLayer);
212
213     var map = this;
214     this._objectLoader = $.ajax({
215       url: OSM.apiUrl(object),
216       dataType: "xml",
217       success: function (xml) {
218         map._objectLayer = new L.OSM.DataLayer(null, {
219           styles: {
220             node: objectStyle,
221             way: objectStyle,
222             area: objectStyle,
223             changeset: changesetStyle
224           }
225         });
226
227         map._objectLayer.interestingNode = function (node, ways, relations) {
228           if (object.type === "node") {
229             return true;
230           } else if (object.type === "relation") {
231             for (var i = 0; i < relations.length; i++)
232               if (relations[i].members.indexOf(node) !== -1)
233                 return true;
234           } else {
235             return false;
236           }
237         };
238
239         map._objectLayer.addData(xml);
240         map._objectLayer.addTo(map);
241
242         if (callback) callback(map._objectLayer.getBounds());
243       }
244     });
245   },
246
247   removeObject: function() {
248     this._object = null;
249     if (this._objectLoader) this._objectLoader.abort();
250     if (this._objectLayer) this.removeLayer(this._objectLayer);
251   },
252
253   getState: function() {
254     return {
255       center: this.getCenter().wrap(),
256       zoom: this.getZoom(),
257       layers: this.getLayersCode()
258     };
259   },
260
261   setState: function(state, options) {
262     if (state.center) this.setView(state.center, state.zoom, options);
263     if (state.layers) this.updateLayers(state.layers);
264   },
265
266   setSidebarOverlaid: function(overlaid) {
267     if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
268       $("#content").addClass("overlay-sidebar");
269       this.invalidateSize({pan: false})
270         .panBy([-350, 0], {animate: false});
271     } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
272       this.panBy([350, 0], {animate: false});
273       $("#content").removeClass("overlay-sidebar");
274       this.invalidateSize({pan: false});
275     }
276     return this;
277   }
278 });
279
280 L.Icon.Default.imagePath = "/images";
281
282 L.Icon.Default.imageUrls = {
283   "/images/marker-icon.png": OSM.MARKER_ICON,
284   "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
285   "/images/marker-shadow.png": OSM.MARKER_SHADOW
286 };
287
288 L.extend(L.Icon.Default.prototype, {
289   _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
290
291   _getIconUrl:  function (name) {
292     var url = this._oldGetIconUrl(name);
293     return L.Icon.Default.imageUrls[url];
294   }
295 });
296
297 OSM.getUserIcon = function (url) {
298   return L.icon({
299     iconUrl: url || OSM.MARKER_RED,
300     iconSize: [25, 41],
301     iconAnchor: [12, 41],
302     popupAnchor: [1, -34],
303     shadowUrl: OSM.MARKER_SHADOW,
304     shadowSize: [41, 41]
305   });
306 };