]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/leaflet.extend.js.erb
Include layers in hash
[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       params.mlat = marker.getLatLng().lat.toFixed(precision);
37       params.mlon = marker.getLatLng().lng.toFixed(precision);
38     }
39
40     var url = 'http://' + OSM.SERVER_URL + '/',
41       query = querystring.stringify(params),
42       hash = OSM.formatHash(this);
43
44     if (query) url += '?' + query;
45     if (hash) url += hash;
46
47     return url;
48   },
49
50   getShortUrl: function(marker) {
51     var zoom = this.getZoom(),
52       latLng = marker && this.hasLayer(marker) ? marker.getLatLng() : this.getCenter(),
53       str = '',
54       char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
55       x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
56       y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
57       // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
58       // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
59       // and drops the last 4 bits of the full 64 bit Morton code.
60       c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
61       digit;
62
63     for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
64       digit = (c1 >> (24 - 6 * i)) & 0x3f;
65       str += char_array.charAt(digit);
66     }
67     for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
68       digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
69       str += char_array.charAt(digit);
70     }
71     for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
72
73     // Called to interlace the bits in x and y, making a Morton code.
74     function interlace(x, y) {
75       x = (x | (x << 8)) & 0x00ff00ff;
76       x = (x | (x << 4)) & 0x0f0f0f0f;
77       x = (x | (x << 2)) & 0x33333333;
78       x = (x | (x << 1)) & 0x55555555;
79       y = (y | (y << 8)) & 0x00ff00ff;
80       y = (y | (y << 4)) & 0x0f0f0f0f;
81       y = (y | (y << 2)) & 0x33333333;
82       y = (y | (y << 1)) & 0x55555555;
83       return (x << 1) | y;
84     }
85
86     if (marker && this.hasLayer(marker)) {
87       str += '?m'
88     }
89
90     return (window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
91             'http://osm.org/go/' : 'http://' + window.location.hostname + '/go/') + str;
92   }
93 });
94
95 L.Icon.Default.imagePath = <%= "#{asset_prefix}/images".to_json %>;
96
97 L.Hash.prototype.parseHash = OSM.parseHash;
98 L.Hash.prototype.formatHash = OSM.formatHash;