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>",
30 name: I18n.t("javascripts.map.base.cycle_map")
32 new L.OSM.TransportMap({
33 attribution: copyright + ". Tiles courtesy of <a href='http://www.thunderforest.com/' target='_blank'>Andy Allan</a>",
35 keyid: "transportmap",
36 name: I18n.t("javascripts.map.base.transport_map")
39 attribution: copyright + ". Tiles courtesy of <a href='http://hot.openstreetmap.org/' target='_blank'>Humanitarian OpenStreetMap Team</a>",
42 name: I18n.t("javascripts.map.base.hot")
46 this.noteLayer = new L.FeatureGroup();
47 this.noteLayer.options = {code: 'N'};
49 this.dataLayer = new L.OSM.DataLayer(null);
50 this.dataLayer.options.code = 'D';
53 updateLayers: function(layerParam) {
54 layerParam = layerParam || "M";
57 for (var i = this.baseLayers.length - 1; i >= 0; i--) {
58 if (layerParam.indexOf(this.baseLayers[i].options.code) >= 0) {
59 this.addLayer(this.baseLayers[i]);
60 layersAdded = layersAdded + this.baseLayers[i].options.code;
61 } else if (i === 0 && layersAdded === "") {
62 this.addLayer(this.baseLayers[i]);
64 this.removeLayer(this.baseLayers[i]);
69 getLayersCode: function () {
71 for (var i in this._layers) { // TODO: map.eachLayer
72 var layer = this._layers[i];
73 if (layer.options && layer.options.code) {
74 layerConfig += layer.options.code;
80 getMapBaseLayerId: function () {
81 for (var i in this._layers) { // TODO: map.eachLayer
82 var layer = this._layers[i];
83 if (layer.options && layer.options.keyid) return layer.options.keyid;
87 getUrl: function(marker) {
88 var precision = OSM.zoomPrecision(this.getZoom()),
91 if (marker && this.hasLayer(marker)) {
92 var latLng = marker.getLatLng().wrap();
93 params.mlat = latLng.lat.toFixed(precision);
94 params.mlon = latLng.lng.toFixed(precision);
97 var url = 'http://' + OSM.SERVER_URL + '/',
98 query = querystring.stringify(params),
99 hash = OSM.formatHash(this);
101 if (query) url += '?' + query;
102 if (hash) url += hash;
107 getShortUrl: function(marker) {
108 var zoom = this.getZoom(),
109 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
110 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
111 'http://osm.org/go/' : 'http://' + window.location.hostname + '/go/',
112 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
113 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
114 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
115 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
116 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
117 // and drops the last 4 bits of the full 64 bit Morton code.
118 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
121 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
122 digit = (c1 >> (24 - 6 * i)) & 0x3f;
123 str += char_array.charAt(digit);
125 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
126 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
127 str += char_array.charAt(digit);
129 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
131 // Called to interlace the bits in x and y, making a Morton code.
132 function interlace(x, y) {
133 x = (x | (x << 8)) & 0x00ff00ff;
134 x = (x | (x << 4)) & 0x0f0f0f0f;
135 x = (x | (x << 2)) & 0x33333333;
136 x = (x | (x << 1)) & 0x55555555;
137 y = (y | (y << 8)) & 0x00ff00ff;
138 y = (y | (y << 4)) & 0x0f0f0f0f;
139 y = (y | (y << 2)) & 0x33333333;
140 y = (y | (y << 1)) & 0x55555555;
145 var layers = this.getLayersCode().replace('M', '');
148 params.layers = layers;
151 if (marker && this.hasLayer(marker)) {
156 params[this._object.type] = this._object.id;
159 var query = querystring.stringify(params);
167 getGeoUri: function(marker) {
168 var precision = OSM.zoomPrecision(this.getZoom()),
172 if (marker && this.hasLayer(marker)) {
173 latLng = marker.getLatLng().wrap();
175 latLng = this.getCenter();
178 params.lat = latLng.lat.toFixed(precision);
179 params.lon = latLng.lng.toFixed(precision);
180 params.zoom = this.getZoom();
182 return 'geo:' + params.lat + ',' + params.lon + '?z=' + params.zoom;
185 addObject: function(object, callback) {
193 var changesetStyle = {
201 this._object = object;
203 if (this._objectLoader) this._objectLoader.abort();
204 if (this._objectLayer) this.removeLayer(this._objectLayer);
207 this._objectLoader = $.ajax({
208 url: OSM.apiUrl(object),
210 success: function (xml) {
211 map._objectLayer = new L.OSM.DataLayer(null, {
216 changeset: changesetStyle
220 map._objectLayer.interestingNode = function (node, ways, relations) {
221 if (object.type === "node") {
223 } else if (object.type === "relation") {
224 for (var i = 0; i < relations.length; i++)
225 if (relations[i].members.indexOf(node) !== -1)
232 map._objectLayer.addData(xml);
233 map._objectLayer.addTo(map);
235 if (callback) callback(map._objectLayer.getBounds());
240 removeObject: function() {
242 if (this._objectLoader) this._objectLoader.abort();
243 if (this._objectLayer) this.removeLayer(this._objectLayer);
246 getState: function() {
248 center: this.getCenter().wrap(),
249 zoom: this.getZoom(),
250 layers: this.getLayersCode()
254 setState: function(state, options) {
255 if (state.center) this.setView(state.center, state.zoom, options);
256 if (state.layers) this.updateLayers(state.layers);
259 setSidebarOverlaid: function(overlaid) {
260 if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
261 $("#content").addClass("overlay-sidebar");
262 this.invalidateSize({pan: false})
263 .panBy([-350, 0], {animate: false});
264 } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
265 this.panBy([350, 0], {animate: false});
266 $("#content").removeClass("overlay-sidebar");
267 this.invalidateSize({pan: false});
273 L.Icon.Default.imagePath = "/images";
275 L.Icon.Default.imageUrls = {
276 "/images/marker-icon.png": OSM.MARKER_ICON,
277 "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
278 "/images/marker-shadow.png": OSM.MARKER_SHADOW
281 L.extend(L.Icon.Default.prototype, {
282 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
284 _getIconUrl: function (name) {
285 var url = this._oldGetIconUrl(name);
286 return L.Icon.Default.imageUrls[url];
290 OSM.getUserIcon = function (url) {
292 iconUrl: url || OSM.MARKER_RED,
294 iconAnchor: [12, 41],
295 popupAnchor: [1, -34],
296 shadowUrl: OSM.MARKER_SHADOW,