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';
60 updateLayers: function(layerParam) {
61 layerParam = layerParam || "M";
64 for (var i = this.baseLayers.length - 1; i >= 0; i--) {
65 if (layerParam.indexOf(this.baseLayers[i].options.code) >= 0) {
66 this.addLayer(this.baseLayers[i]);
67 layersAdded = layersAdded + this.baseLayers[i].options.code;
68 } else if (i === 0 && layersAdded === "") {
69 this.addLayer(this.baseLayers[i]);
71 this.removeLayer(this.baseLayers[i]);
76 getLayersCode: function () {
78 for (var i in this._layers) { // TODO: map.eachLayer
79 var layer = this._layers[i];
80 if (layer.options && layer.options.code) {
81 layerConfig += layer.options.code;
87 getMapBaseLayerId: function () {
88 for (var i in this._layers) { // TODO: map.eachLayer
89 var layer = this._layers[i];
90 if (layer.options && layer.options.keyid) return layer.options.keyid;
94 getUrl: function(marker) {
95 var precision = OSM.zoomPrecision(this.getZoom()),
98 if (marker && this.hasLayer(marker)) {
99 var latLng = marker.getLatLng().wrap();
100 params.mlat = latLng.lat.toFixed(precision);
101 params.mlon = latLng.lng.toFixed(precision);
104 var url = 'http://' + OSM.SERVER_URL + '/',
105 query = querystring.stringify(params),
106 hash = OSM.formatHash(this);
108 if (query) url += '?' + query;
109 if (hash) url += hash;
114 getShortUrl: function(marker) {
115 var zoom = this.getZoom(),
116 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
117 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
118 'http://osm.org/go/' : 'http://' + window.location.hostname + '/go/',
119 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
120 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
121 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
122 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
123 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
124 // and drops the last 4 bits of the full 64 bit Morton code.
125 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
128 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
129 digit = (c1 >> (24 - 6 * i)) & 0x3f;
130 str += char_array.charAt(digit);
132 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
133 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
134 str += char_array.charAt(digit);
136 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
138 // Called to interlace the bits in x and y, making a Morton code.
139 function interlace(x, y) {
140 x = (x | (x << 8)) & 0x00ff00ff;
141 x = (x | (x << 4)) & 0x0f0f0f0f;
142 x = (x | (x << 2)) & 0x33333333;
143 x = (x | (x << 1)) & 0x55555555;
144 y = (y | (y << 8)) & 0x00ff00ff;
145 y = (y | (y << 4)) & 0x0f0f0f0f;
146 y = (y | (y << 2)) & 0x33333333;
147 y = (y | (y << 1)) & 0x55555555;
152 var layers = this.getLayersCode().replace('M', '');
155 params.layers = layers;
158 if (marker && this.hasLayer(marker)) {
163 params[this._object.type] = this._object.id;
166 var query = querystring.stringify(params);
174 getGeoUri: function(marker) {
175 var precision = OSM.zoomPrecision(this.getZoom()),
179 if (marker && this.hasLayer(marker)) {
180 latLng = marker.getLatLng().wrap();
182 latLng = this.getCenter();
185 params.lat = latLng.lat.toFixed(precision);
186 params.lon = latLng.lng.toFixed(precision);
187 params.zoom = this.getZoom();
189 return 'geo:' + params.lat + ',' + params.lon + '?z=' + params.zoom;
192 addObject: function(object, callback) {
200 var changesetStyle = {
208 this._object = object;
210 if (this._objectLoader) this._objectLoader.abort();
211 if (this._objectLayer) this.removeLayer(this._objectLayer);
214 this._objectLoader = $.ajax({
215 url: OSM.apiUrl(object),
217 success: function (xml) {
218 map._objectLayer = new L.OSM.DataLayer(null, {
223 changeset: changesetStyle
227 map._objectLayer.interestingNode = function (node, ways, relations) {
228 if (object.type === "node") {
230 } else if (object.type === "relation") {
231 for (var i = 0; i < relations.length; i++)
232 if (relations[i].members.indexOf(node) !== -1)
239 map._objectLayer.addData(xml);
240 map._objectLayer.addTo(map);
242 if (callback) callback(map._objectLayer.getBounds());
247 removeObject: function() {
249 if (this._objectLoader) this._objectLoader.abort();
250 if (this._objectLayer) this.removeLayer(this._objectLayer);
253 getState: function() {
255 center: this.getCenter().wrap(),
256 zoom: this.getZoom(),
257 layers: this.getLayersCode()
261 setState: function(state, options) {
262 if (state.center) this.setView(state.center, state.zoom, options);
263 if (state.layers) this.updateLayers(state.layers);
266 setSidebarOverlaid: function(overlaid) {
267 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
268 $("#content").addClass("overlay-sidebar");
269 this.invalidateSize({pan: false})
270 .panBy([-350, 0], {animate: false});
271 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
272 this.panBy([350, 0], {animate: false});
273 $("#content").removeClass("overlay-sidebar");
274 this.invalidateSize({pan: false});
280 L.Icon.Default.imagePath = "/images";
282 L.Icon.Default.imageUrls = {
283 "/images/marker-icon.png": OSM.MARKER_ICON,
284 "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
285 "/images/marker-shadow.png": OSM.MARKER_SHADOW
288 L.extend(L.Icon.Default.prototype, {
289 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
291 _getIconUrl: function (name) {
292 var url = this._oldGetIconUrl(name);
293 return L.Icon.Default.imageUrls[url];
297 OSM.getUserIcon = function (url) {
299 iconUrl: url || OSM.MARKER_RED,
301 iconAnchor: [12, 41],
302 popupAnchor: [1, -34],
303 shadowUrl: OSM.MARKER_SHADOW,