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 = 'http://' + 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 'http://osm.org/go/' : 'http://' + window.location.hostname + '/go/',
125 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
126 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
127 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
128 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
129 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
130 // and drops the last 4 bits of the full 64 bit Morton code.
131 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
134 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
135 digit = (c1 >> (24 - 6 * i)) & 0x3f;
136 str += char_array.charAt(digit);
138 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
139 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
140 str += char_array.charAt(digit);
142 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
144 // Called to interlace the bits in x and y, making a Morton code.
145 function interlace(x, y) {
146 x = (x | (x << 8)) & 0x00ff00ff;
147 x = (x | (x << 4)) & 0x0f0f0f0f;
148 x = (x | (x << 2)) & 0x33333333;
149 x = (x | (x << 1)) & 0x55555555;
150 y = (y | (y << 8)) & 0x00ff00ff;
151 y = (y | (y << 4)) & 0x0f0f0f0f;
152 y = (y | (y << 2)) & 0x33333333;
153 y = (y | (y << 1)) & 0x55555555;
158 var layers = this.getLayersCode().replace('M', '');
161 params.layers = layers;
164 if (marker && this.hasLayer(marker)) {
169 params[this._object.type] = this._object.id;
172 var query = querystring.stringify(params);
180 getGeoUri: function(marker) {
181 var precision = OSM.zoomPrecision(this.getZoom()),
185 if (marker && this.hasLayer(marker)) {
186 latLng = marker.getLatLng().wrap();
188 latLng = this.getCenter();
191 params.lat = latLng.lat.toFixed(precision);
192 params.lon = latLng.lng.toFixed(precision);
193 params.zoom = this.getZoom();
195 return 'geo:' + params.lat + ',' + params.lon + '?z=' + params.zoom;
198 addObject: function(object, callback) {
206 var changesetStyle = {
217 this._objectLoader = $.ajax({
218 url: OSM.apiUrl(object),
220 success: function (xml) {
221 map._object = object;
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,