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