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 attribution: copyright + " ♥ " + donate,
24 name: I18n.t("javascripts.map.base.standard")
27 attribution: copyright + ". Tiles courtesy of <a href='http://www.thunderforest.com/' target='_blank'>Andy Allan</a>",
28 apikey: OSM.THUNDERFOREST_KEY,
31 name: I18n.t("javascripts.map.base.cycle_map")
33 new L.OSM.TransportMap({
34 attribution: copyright + ". Tiles courtesy of <a href='http://www.thunderforest.com/' target='_blank'>Andy Allan</a>",
35 apikey: OSM.THUNDERFOREST_KEY,
37 keyid: "transportmap",
38 name: I18n.t("javascripts.map.base.transport_map")
41 attribution: copyright + ". Tiles courtesy of <a href='http://hot.openstreetmap.org/' target='_blank'>Humanitarian OpenStreetMap Team</a>",
44 name: I18n.t("javascripts.map.base.hot")
48 this.noteLayer = new L.FeatureGroup();
49 this.noteLayer.options = {code: 'N'};
51 this.dataLayer = new L.OSM.DataLayer(null);
52 this.dataLayer.options.code = 'D';
55 updateLayers: function(layerParam) {
56 layerParam = layerParam || "M";
59 for (var i = this.baseLayers.length - 1; i >= 0; i--) {
60 if (layerParam.indexOf(this.baseLayers[i].options.code) >= 0) {
61 this.addLayer(this.baseLayers[i]);
62 layersAdded = layersAdded + this.baseLayers[i].options.code;
63 } else if (i === 0 && layersAdded === "") {
64 this.addLayer(this.baseLayers[i]);
66 this.removeLayer(this.baseLayers[i]);
71 getLayersCode: function () {
73 for (var i in this._layers) { // TODO: map.eachLayer
74 var layer = this._layers[i];
75 if (layer.options && layer.options.code) {
76 layerConfig += layer.options.code;
82 getMapBaseLayerId: function () {
83 for (var i in this._layers) { // TODO: map.eachLayer
84 var layer = this._layers[i];
85 if (layer.options && layer.options.keyid) return layer.options.keyid;
89 getUrl: function(marker) {
90 var precision = OSM.zoomPrecision(this.getZoom()),
93 if (marker && this.hasLayer(marker)) {
94 var latLng = marker.getLatLng().wrap();
95 params.mlat = latLng.lat.toFixed(precision);
96 params.mlon = latLng.lng.toFixed(precision);
99 var url = 'http://' + OSM.SERVER_URL + '/',
100 query = querystring.stringify(params),
101 hash = OSM.formatHash(this);
103 if (query) url += '?' + query;
104 if (hash) url += hash;
109 getShortUrl: function(marker) {
110 var zoom = this.getZoom(),
111 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
112 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
113 'http://osm.org/go/' : 'http://' + window.location.hostname + '/go/',
114 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
115 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
116 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
117 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
118 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
119 // and drops the last 4 bits of the full 64 bit Morton code.
120 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
123 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
124 digit = (c1 >> (24 - 6 * i)) & 0x3f;
125 str += char_array.charAt(digit);
127 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
128 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
129 str += char_array.charAt(digit);
131 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
133 // Called to interlace the bits in x and y, making a Morton code.
134 function interlace(x, y) {
135 x = (x | (x << 8)) & 0x00ff00ff;
136 x = (x | (x << 4)) & 0x0f0f0f0f;
137 x = (x | (x << 2)) & 0x33333333;
138 x = (x | (x << 1)) & 0x55555555;
139 y = (y | (y << 8)) & 0x00ff00ff;
140 y = (y | (y << 4)) & 0x0f0f0f0f;
141 y = (y | (y << 2)) & 0x33333333;
142 y = (y | (y << 1)) & 0x55555555;
147 var layers = this.getLayersCode().replace('M', '');
150 params.layers = layers;
153 if (marker && this.hasLayer(marker)) {
158 params[this._object.type] = this._object.id;
161 var query = querystring.stringify(params);
169 getGeoUri: function(marker) {
170 var precision = OSM.zoomPrecision(this.getZoom()),
174 if (marker && this.hasLayer(marker)) {
175 latLng = marker.getLatLng().wrap();
177 latLng = this.getCenter();
180 params.lat = latLng.lat.toFixed(precision);
181 params.lon = latLng.lng.toFixed(precision);
182 params.zoom = this.getZoom();
184 return 'geo:' + params.lat + ',' + params.lon + '?z=' + params.zoom;
187 addObject: function(object, callback) {
195 var changesetStyle = {
203 this._object = object;
205 if (this._objectLoader) this._objectLoader.abort();
206 if (this._objectLayer) this.removeLayer(this._objectLayer);
209 this._objectLoader = $.ajax({
210 url: OSM.apiUrl(object),
212 success: function (xml) {
213 map._objectLayer = new L.OSM.DataLayer(null, {
218 changeset: changesetStyle
222 map._objectLayer.interestingNode = function (node, ways, relations) {
223 if (object.type === "node") {
225 } else if (object.type === "relation") {
226 for (var i = 0; i < relations.length; i++)
227 if (relations[i].members.indexOf(node) !== -1)
234 map._objectLayer.addData(xml);
235 map._objectLayer.addTo(map);
237 if (callback) callback(map._objectLayer.getBounds());
242 removeObject: function() {
244 if (this._objectLoader) this._objectLoader.abort();
245 if (this._objectLayer) this.removeLayer(this._objectLayer);
248 getState: function() {
250 center: this.getCenter().wrap(),
251 zoom: this.getZoom(),
252 layers: this.getLayersCode()
256 setState: function(state, options) {
257 if (state.center) this.setView(state.center, state.zoom, options);
258 if (state.layers) this.updateLayers(state.layers);
261 setSidebarOverlaid: function(overlaid) {
262 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
263 $("#content").addClass("overlay-sidebar");
264 this.invalidateSize({pan: false})
265 .panBy([-350, 0], {animate: false});
266 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
267 this.panBy([350, 0], {animate: false});
268 $("#content").removeClass("overlay-sidebar");
269 this.invalidateSize({pan: false});
275 L.Icon.Default.imagePath = "/images";
277 L.Icon.Default.imageUrls = {
278 "/images/marker-icon.png": OSM.MARKER_ICON,
279 "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
280 "/images/marker-shadow.png": OSM.MARKER_SHADOW
283 L.extend(L.Icon.Default.prototype, {
284 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
286 _getIconUrl: function (name) {
287 var url = this._oldGetIconUrl(name);
288 return L.Icon.Default.imageUrls[url];
292 OSM.getUserIcon = function (url) {
294 iconUrl: url || OSM.MARKER_RED,
296 iconAnchor: [12, 41],
297 popupAnchor: [1, -34],
298 shadowUrl: OSM.MARKER_SHADOW,