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")
38 new L.OSM.MapQuestOpen({
39 attribution: copyright + ". Tiles courtesy of <a href='http://www.mapquest.com/' target='_blank'>MapQuest</a> <img src='https://developer.mapquest.com/content/osm/mq_logo.png'>",
42 name: I18n.t("javascripts.map.base.mapquest")
45 attribution: copyright + ". Tiles courtesy of <a href='http://hot.openstreetmap.org/' target='_blank'>Humanitarian OpenStreetMap Team</a>",
48 name: I18n.t("javascripts.map.base.hot")
52 this.noteLayer = new L.FeatureGroup();
53 this.noteLayer.options = {code: 'N'};
55 this.dataLayer = new L.OSM.DataLayer(null);
56 this.dataLayer.options.code = 'D';
59 updateLayers: function(layerParam) {
60 layerParam = layerParam || "M";
63 for (var i = this.baseLayers.length - 1; i >= 0; i--) {
64 if (layerParam.indexOf(this.baseLayers[i].options.code) >= 0) {
65 this.addLayer(this.baseLayers[i]);
66 layersAdded = layersAdded + this.baseLayers[i].options.code;
67 } else if (i == 0 && layersAdded == "") {
68 this.addLayer(this.baseLayers[i]);
70 this.removeLayer(this.baseLayers[i]);
75 getLayersCode: function () {
77 for (var i in this._layers) { // TODO: map.eachLayer
78 var layer = this._layers[i];
79 if (layer.options && layer.options.code) {
80 layerConfig += layer.options.code;
86 getMapBaseLayerId: function () {
87 for (var i in this._layers) { // TODO: map.eachLayer
88 var layer = this._layers[i];
89 if (layer.options && layer.options.keyid) return layer.options.keyid;
93 getUrl: function(marker) {
94 var precision = OSM.zoomPrecision(this.getZoom()),
97 if (marker && this.hasLayer(marker)) {
98 var latLng = marker.getLatLng().wrap();
99 params.mlat = latLng.lat.toFixed(precision);
100 params.mlon = latLng.lng.toFixed(precision);
103 var url = 'http://' + OSM.SERVER_URL + '/',
104 query = querystring.stringify(params),
105 hash = OSM.formatHash(this);
107 if (query) url += '?' + query;
108 if (hash) url += hash;
113 getShortUrl: function(marker) {
114 var zoom = this.getZoom(),
115 latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
116 str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
117 'http://osm.org/go/' : 'http://' + window.location.hostname + '/go/',
118 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
119 x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
120 y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
121 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
122 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
123 // and drops the last 4 bits of the full 64 bit Morton code.
124 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
127 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
128 digit = (c1 >> (24 - 6 * i)) & 0x3f;
129 str += char_array.charAt(digit);
131 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
132 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
133 str += char_array.charAt(digit);
135 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
137 // Called to interlace the bits in x and y, making a Morton code.
138 function interlace(x, y) {
139 x = (x | (x << 8)) & 0x00ff00ff;
140 x = (x | (x << 4)) & 0x0f0f0f0f;
141 x = (x | (x << 2)) & 0x33333333;
142 x = (x | (x << 1)) & 0x55555555;
143 y = (y | (y << 8)) & 0x00ff00ff;
144 y = (y | (y << 4)) & 0x0f0f0f0f;
145 y = (y | (y << 2)) & 0x33333333;
146 y = (y | (y << 1)) & 0x55555555;
151 var layers = this.getLayersCode().replace('M', '');
154 params.layers = layers;
157 if (marker && this.hasLayer(marker)) {
162 params[this._object.type] = this._object.id;
165 var query = querystring.stringify(params);
173 addObject: function(object, callback) {
181 var changesetStyle = {
189 this._object = object;
191 if (this._objectLoader) this._objectLoader.abort();
192 if (this._objectLayer) this.removeLayer(this._objectLayer);
195 this._objectLoader = $.ajax({
196 url: OSM.apiUrl(object),
198 success: function (xml) {
199 map._objectLayer = new L.OSM.DataLayer(null, {
204 changeset: changesetStyle
208 map._objectLayer.interestingNode = function (node, ways, relations) {
209 if (object.type === "node") {
211 } else if (object.type === "relation") {
212 for (var i = 0; i < relations.length; i++)
213 if (relations[i].members.indexOf(node) != -1)
220 map._objectLayer.addData(xml);
221 map._objectLayer.addTo(map);
223 if (callback) callback(map._objectLayer.getBounds());
228 removeObject: function() {
230 if (this._objectLoader) this._objectLoader.abort();
231 if (this._objectLayer) this.removeLayer(this._objectLayer);
234 getState: function() {
236 center: this.getCenter().wrap(),
237 zoom: this.getZoom(),
238 layers: this.getLayersCode()
242 setState: function(state, options) {
243 if (state.center) this.setView(state.center, state.zoom, options);
244 if (state.layers) this.updateLayers(state.layers);
248 L.Icon.Default.imagePath = "/images";
250 L.Icon.Default.imageUrls = {
251 "/images/marker-icon.png": "<%= asset_path("images/marker-icon.png") %>",
252 "/images/marker-icon-2x.png": "<%= asset_path("images/marker-icon-2x.png") %>",
253 "/images/marker-shadow.png": "<%= asset_path("images/marker-shadow.png") %>"
256 L.extend(L.Icon.Default.prototype, {
257 _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
259 _getIconUrl: function (name) {
260 var url = this._oldGetIconUrl(name);
261 return L.Icon.Default.imageUrls[url];
265 function getUserIcon(url) {
267 iconUrl: url || <%= asset_path('marker-red.png').to_json %>,
269 iconAnchor: [12, 41],
270 popupAnchor: [1, -34],
271 shadowUrl: <%= asset_path('images/marker-shadow.png').to_json %>,