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: 'http://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='http://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='http://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 courtesy of <a href='http://hot.openstreetmap.org/' target='_blank'>Humanitarian OpenStreetMap Team</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({
61 name: I18n.t("javascripts.map.base.gps")
65 updateLayers: function(layerParam) {
66 layerParam = layerParam || "M";
69 for (var i = this.baseLayers.length - 1; i >= 0; i--) {
70 if (layerParam.indexOf(this.baseLayers[i].options.code) >= 0) {
71 this.addLayer(this.baseLayers[i]);
72 layersAdded = layersAdded + this.baseLayers[i].options.code;
73 } else if (i === 0 && layersAdded === "") {
74 this.addLayer(this.baseLayers[i]);
76 this.removeLayer(this.baseLayers[i]);
81 getLayersCode: function () {
83 for (var i in this._layers) { // TODO: map.eachLayer
84 var layer = this._layers[i];
85 if (layer.options && layer.options.code) {
86 layerConfig += layer.options.code;
92 getMapBaseLayerId: function () {
93 for (var i in this._layers) { // TODO: map.eachLayer
94 var layer = this._layers[i];
95 if (layer.options && layer.options.keyid) return layer.options.keyid;
99 getUrl: function(marker) {
100 var precision = OSM.zoomPrecision(this.getZoom()),
103 if (marker && this.hasLayer(marker)) {
104 var latLng = marker.getLatLng().wrap();
105 params.mlat = latLng.lat.toFixed(precision);
106 params.mlon = latLng.lng.toFixed(precision);
109 var url = 'http://' + OSM.SERVER_URL + '/',
110 query = querystring.stringify(params),
111 hash = OSM.formatHash(this);
113 if (query) url += '?' + query;
114 if (hash) url += hash;
119 getShortUrl: function(marker) {
120 var zoom = this.getZoom(),
121 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
122 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
123 'http://osm.org/go/' : 'http://' + window.location.hostname + '/go/',
124 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
125 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
126 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
127 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
128 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
129 // and drops the last 4 bits of the full 64 bit Morton code.
130 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
133 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
134 digit = (c1 >> (24 - 6 * i)) & 0x3f;
135 str += char_array.charAt(digit);
137 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
138 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
139 str += char_array.charAt(digit);
141 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
143 // Called to interlace the bits in x and y, making a Morton code.
144 function interlace(x, y) {
145 x = (x | (x << 8)) & 0x00ff00ff;
146 x = (x | (x << 4)) & 0x0f0f0f0f;
147 x = (x | (x << 2)) & 0x33333333;
148 x = (x | (x << 1)) & 0x55555555;
149 y = (y | (y << 8)) & 0x00ff00ff;
150 y = (y | (y << 4)) & 0x0f0f0f0f;
151 y = (y | (y << 2)) & 0x33333333;
152 y = (y | (y << 1)) & 0x55555555;
157 var layers = this.getLayersCode().replace('M', '');
160 params.layers = layers;
163 if (marker && this.hasLayer(marker)) {
168 params[this._object.type] = this._object.id;
171 var query = querystring.stringify(params);
179 getGeoUri: function(marker) {
180 var precision = OSM.zoomPrecision(this.getZoom()),
184 if (marker && this.hasLayer(marker)) {
185 latLng = marker.getLatLng().wrap();
187 latLng = this.getCenter();
190 params.lat = latLng.lat.toFixed(precision);
191 params.lon = latLng.lng.toFixed(precision);
192 params.zoom = this.getZoom();
194 return 'geo:' + params.lat + ',' + params.lon + '?z=' + params.zoom;
197 addObject: function(object, callback) {
205 var changesetStyle = {
213 this._object = object;
215 if (this._objectLoader) this._objectLoader.abort();
216 if (this._objectLayer) this.removeLayer(this._objectLayer);
219 this._objectLoader = $.ajax({
220 url: OSM.apiUrl(object),
222 success: function (xml) {
223 map._objectLayer = new L.OSM.DataLayer(null, {
228 changeset: changesetStyle
232 map._objectLayer.interestingNode = function (node, ways, relations) {
233 if (object.type === "node") {
235 } else if (object.type === "relation") {
236 for (var i = 0; i < relations.length; i++)
237 if (relations[i].members.indexOf(node) !== -1)
244 map._objectLayer.addData(xml);
245 map._objectLayer.addTo(map);
247 if (callback) callback(map._objectLayer.getBounds());
252 removeObject: function() {
254 if (this._objectLoader) this._objectLoader.abort();
255 if (this._objectLayer) this.removeLayer(this._objectLayer);
258 getState: function() {
260 center: this.getCenter().wrap(),
261 zoom: this.getZoom(),
262 layers: this.getLayersCode()
266 setState: function(state, options) {
267 if (state.center) this.setView(state.center, state.zoom, options);
268 if (state.layers) this.updateLayers(state.layers);
271 setSidebarOverlaid: function(overlaid) {
272 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
273 $("#content").addClass("overlay-sidebar");
274 this.invalidateSize({pan: false})
275 .panBy([-350, 0], {animate: false});
276 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
277 this.panBy([350, 0], {animate: false});
278 $("#content").removeClass("overlay-sidebar");
279 this.invalidateSize({pan: false});
285 L.Icon.Default.imagePath = "/images/";
287 L.Icon.Default.imageUrls = {
288 "/images/marker-icon.png": OSM.MARKER_ICON,
289 "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
290 "/images/marker-shadow.png": OSM.MARKER_SHADOW
293 L.extend(L.Icon.Default.prototype, {
294 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
296 _getIconUrl: function (name) {
297 var url = this._oldGetIconUrl(name);
298 return L.Icon.Default.imageUrls[url];
302 OSM.getUserIcon = function (url) {
304 iconUrl: url || OSM.MARKER_RED,
306 iconAnchor: [12, 41],
307 popupAnchor: [1, -34],
308 shadowUrl: OSM.MARKER_SHADOW,