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({
62 name: I18n.t("javascripts.map.base.gps")
66 updateLayers: function(layerParam) {
67 layerParam = layerParam || "M";
70 for (var i = this.baseLayers.length - 1; i >= 0; i--) {
71 if (layerParam.indexOf(this.baseLayers[i].options.code) >= 0) {
72 this.addLayer(this.baseLayers[i]);
73 layersAdded = layersAdded + this.baseLayers[i].options.code;
74 } else if (i === 0 && layersAdded === "") {
75 this.addLayer(this.baseLayers[i]);
77 this.removeLayer(this.baseLayers[i]);
82 getLayersCode: function () {
84 for (var i in this._layers) { // TODO: map.eachLayer
85 var layer = this._layers[i];
86 if (layer.options && layer.options.code) {
87 layerConfig += layer.options.code;
93 getMapBaseLayerId: function () {
94 for (var i in this._layers) { // TODO: map.eachLayer
95 var layer = this._layers[i];
96 if (layer.options && layer.options.keyid) return layer.options.keyid;
100 getUrl: function(marker) {
101 var precision = OSM.zoomPrecision(this.getZoom()),
104 if (marker && this.hasLayer(marker)) {
105 var latLng = marker.getLatLng().wrap();
106 params.mlat = latLng.lat.toFixed(precision);
107 params.mlon = latLng.lng.toFixed(precision);
110 var url = window.location.protocol + '//' + OSM.SERVER_URL + '/',
111 query = querystring.stringify(params),
112 hash = OSM.formatHash(this);
114 if (query) url += '?' + query;
115 if (hash) url += hash;
120 getShortUrl: function(marker) {
121 var zoom = this.getZoom(),
122 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
123 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
124 window.location.protocol + '//osm.org/go/' :
125 window.location.protocol + '//' + window.location.hostname + '/go/',
126 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
127 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
128 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
129 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
130 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
131 // and drops the last 4 bits of the full 64 bit Morton code.
132 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
135 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
136 digit = (c1 >> (24 - 6 * i)) & 0x3f;
137 str += char_array.charAt(digit);
139 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
140 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
141 str += char_array.charAt(digit);
143 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
145 // Called to interlace the bits in x and y, making a Morton code.
146 function interlace(x, y) {
147 x = (x | (x << 8)) & 0x00ff00ff;
148 x = (x | (x << 4)) & 0x0f0f0f0f;
149 x = (x | (x << 2)) & 0x33333333;
150 x = (x | (x << 1)) & 0x55555555;
151 y = (y | (y << 8)) & 0x00ff00ff;
152 y = (y | (y << 4)) & 0x0f0f0f0f;
153 y = (y | (y << 2)) & 0x33333333;
154 y = (y | (y << 1)) & 0x55555555;
159 var layers = this.getLayersCode().replace('M', '');
162 params.layers = layers;
165 if (marker && this.hasLayer(marker)) {
170 params[this._object.type] = this._object.id;
173 var query = querystring.stringify(params);
181 getGeoUri: function(marker) {
182 var precision = OSM.zoomPrecision(this.getZoom()),
186 if (marker && this.hasLayer(marker)) {
187 latLng = marker.getLatLng().wrap();
189 latLng = this.getCenter();
192 params.lat = latLng.lat.toFixed(precision);
193 params.lon = latLng.lng.toFixed(precision);
194 params.zoom = this.getZoom();
196 return 'geo:' + params.lat + ',' + params.lon + '?z=' + params.zoom;
199 addObject: function(object, callback) {
207 var changesetStyle = {
218 this._objectLoader = $.ajax({
219 url: OSM.apiUrl(object),
221 success: function (xml) {
222 map._object = object;
224 map._objectLayer = new L.OSM.DataLayer(null, {
229 changeset: changesetStyle
233 map._objectLayer.interestingNode = function (node, ways, relations) {
234 if (object.type === "node") {
236 } else if (object.type === "relation") {
237 for (var i = 0; i < relations.length; i++)
238 if (relations[i].members.indexOf(node) !== -1)
245 map._objectLayer.addData(xml);
246 map._objectLayer.addTo(map);
248 if (callback) callback(map._objectLayer.getBounds());
253 removeObject: function() {
255 if (this._objectLoader) this._objectLoader.abort();
256 if (this._objectLayer) this.removeLayer(this._objectLayer);
259 getState: function() {
261 center: this.getCenter().wrap(),
262 zoom: this.getZoom(),
263 layers: this.getLayersCode()
267 setState: function(state, options) {
268 if (state.center) this.setView(state.center, state.zoom, options);
269 if (state.layers) this.updateLayers(state.layers);
272 setSidebarOverlaid: function(overlaid) {
273 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
274 $("#content").addClass("overlay-sidebar");
275 this.invalidateSize({pan: false})
276 .panBy([-350, 0], {animate: false});
277 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
278 this.panBy([350, 0], {animate: false});
279 $("#content").removeClass("overlay-sidebar");
280 this.invalidateSize({pan: false});
286 L.Icon.Default.imagePath = "/images/";
288 L.Icon.Default.imageUrls = {
289 "/images/marker-icon.png": OSM.MARKER_ICON,
290 "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
291 "/images/marker-shadow.png": OSM.MARKER_SHADOW
294 L.extend(L.Icon.Default.prototype, {
295 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
297 _getIconUrl: function (name) {
298 var url = this._oldGetIconUrl(name);
299 return L.Icon.Default.imageUrls[url];
303 OSM.getUserIcon = function (url) {
305 iconUrl: url || OSM.MARKER_RED,
307 iconAnchor: [12, 41],
308 popupAnchor: [1, -34],
309 shadowUrl: OSM.MARKER_SHADOW,