]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/leaflet.extend.js.erb
Fix display of browsed objects on map
[rails.git] / app / assets / javascripts / leaflet.extend.js.erb
1 L.extend(L.LatLngBounds.prototype, {
2   getSize: function () {
3     return (this._northEast.lat - this._southWest.lat) *
4            (this._northEast.lng - this._southWest.lng);
5   },
6
7   wrap: function () {
8     return new L.LatLngBounds(this._southWest.wrap(), this._northEast.wrap());
9   }
10 });
11
12 L.extend(L.Map.prototype, {
13   getLayersCode: function () {
14     var layerConfig = '';
15     for (var i in this._layers) { // TODO: map.eachLayer
16       var layer = this._layers[i];
17       if (layer.options && layer.options.code) {
18         layerConfig += layer.options.code;
19       }
20     }
21     return layerConfig;
22   },
23
24   getMapBaseLayerId: function () {
25     for (var i in this._layers) { // TODO: map.eachLayer
26       var layer = this._layers[i];
27       if (layer.options && layer.options.keyid) return layer.options.keyid;
28     }
29   },
30
31   getUrl: function(marker) {
32     var precision = zoomPrecision(this.getZoom()),
33         params = {};
34
35     if (marker && this.hasLayer(marker)) {
36       var latLng = marker.getLatLng().wrap();
37       params.mlat = latLng.lat.toFixed(precision);
38       params.mlon = latLng.lng.toFixed(precision);
39     }
40
41     if (this._object) {
42       params[this._object.type] = this._object.id;
43     }
44
45     var url = 'http://' + OSM.SERVER_URL + '/',
46       query = querystring.stringify(params),
47       hash = OSM.formatHash(this);
48
49     if (query) url += '?' + query;
50     if (hash) url += hash;
51
52     return url;
53   },
54
55   getShortUrl: function(marker) {
56     var zoom = this.getZoom(),
57       latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
58       str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
59         'http://osm.org/go/' : 'http://' + window.location.hostname + '/go/',
60       char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
61       x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
62       y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
63       // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
64       // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
65       // and drops the last 4 bits of the full 64 bit Morton code.
66       c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
67       digit;
68
69     for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
70       digit = (c1 >> (24 - 6 * i)) & 0x3f;
71       str += char_array.charAt(digit);
72     }
73     for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
74       digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
75       str += char_array.charAt(digit);
76     }
77     for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
78
79     // Called to interlace the bits in x and y, making a Morton code.
80     function interlace(x, y) {
81       x = (x | (x << 8)) & 0x00ff00ff;
82       x = (x | (x << 4)) & 0x0f0f0f0f;
83       x = (x | (x << 2)) & 0x33333333;
84       x = (x | (x << 1)) & 0x55555555;
85       y = (y | (y << 8)) & 0x00ff00ff;
86       y = (y | (y << 4)) & 0x0f0f0f0f;
87       y = (y | (y << 2)) & 0x33333333;
88       y = (y | (y << 1)) & 0x55555555;
89       return (x << 1) | y;
90     }
91
92     var params = {};
93
94     if (marker && this.hasLayer(marker)) {
95       params.m = '';
96     }
97
98     if (this._object) {
99       params[this._object.type] = this._object.id;
100     }
101
102     var query = querystring.stringify(params);
103     if (query) {
104       str += '?' + query;
105     }
106
107     return str;
108   },
109
110   addObject: function(object, options) {
111     this._object = object;
112
113     if (this._objectLoader) this._objectLoader.abort();
114     if (this._objectLayer) this.removeLayer(this._objectLayer);
115
116     var map = this;
117     this._objectLoader = $.ajax({
118       url: OSM.apiUrl(object),
119       dataType: "xml",
120       success: function (xml) {
121         map._objectLayer = new L.OSM.DataLayer(null, {
122           styles: {
123             node: options.style,
124             way: options.style,
125             area: options.style,
126             changeset: {
127               weight: 2,
128               color: '#e90',
129               fillOpacity: 0
130             }
131           }
132         });
133
134         map._objectLayer.interestingNode = function (node, ways, relations) {
135           if (object.type === "node") {
136             return true;
137           } else if (object.type === "relation") {
138             for (var i = 0; i < relations.length; i++)
139               if (relations[i].members.indexOf(node) != -1)
140                 return true;
141           } else {
142             return false;
143           }
144         };
145
146         map._objectLayer.addData(xml);
147
148         var bounds = map._objectLayer.getBounds();
149
150         if (options.zoom && bounds.isValid()) map.fitBounds(bounds);
151         if (options.callback) options.callback(bounds);
152
153         map._objectLayer.addTo(map);
154       }
155     });
156   },
157
158   removeObject: function() {
159     this._object = null;
160     if (this._objectLoader) this._objectLoader.abort();
161     if (this._objectLayer) this.removeLayer(this._objectLayer);
162   }
163 });
164
165 L.Icon.Default.imagePath = "/images";
166
167 L.Icon.Default.imageUrls = {
168   "/images/marker-icon.png": "<%= asset_path("images/marker-icon.png") %>",
169   "/images/marker-icon-2x.png": "<%= asset_path("images/marker-icon-2x.png") %>",
170   "/images/marker-shadow.png": "<%= asset_path("images/marker-shadow.png") %>",
171   "/images/marker-shadow-2x.png": "<%= asset_path("images/marker-shadow-2x.png") %>"
172 };
173
174 L.extend(L.Icon.Default.prototype, {
175   _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
176
177   _getIconUrl:  function (name) {
178     var url = this._oldGetIconUrl(name);
179     return L.Icon.Default.imageUrls[url];
180   }
181 });
182
183 L.Hash.prototype.parseHash = OSM.parseHash;
184 L.Hash.prototype.formatHash = OSM.formatHash;