1a98f70a6b2a0bc248a117e78bb468282bcbc34c
[lhc/web/www.git] / www / plugins / gis / lib / leaflet / plugins / Bing.js
1 L.BingLayer = L.TileLayer.extend({
2 options: {
3 subdomains: [0, 1, 2, 3],
4 type: 'Aerial',
5 attribution: 'Bing',
6 culture: ''
7 },
8
9 initialize: function(key, options) {
10 L.Util.setOptions(this, options);
11
12 this._key = key;
13 this._url = null;
14 this.metaRequested = false;
15 },
16
17 tile2quad: function(x, y, z) {
18 var quad = '';
19 for (var i = z; i > 0; i--) {
20 var digit = 0;
21 var mask = 1 << (i - 1);
22 if ((x & mask) !== 0) digit += 1;
23 if ((y & mask) !== 0) digit += 2;
24 quad = quad + digit;
25 }
26 return quad;
27 },
28
29 getTileUrl: function(p, z) {
30 var zoom = this._getZoomForUrl();
31 var subdomains = this.options.subdomains,
32 s = this.options.subdomains[Math.abs((p.x + p.y) % subdomains.length)];
33 return this._url.replace('{subdomain}', s)
34 .replace('{quadkey}', this.tile2quad(p.x, p.y, zoom))
35 .replace('{culture}', this.options.culture);
36 },
37
38 loadMetadata: function() {
39 if (this.metaRequested) return;
40 this.metaRequested = true;
41 var _this = this;
42 var cbid = '_bing_metadata_' + L.Util.stamp(this);
43 window[cbid] = function (meta) {
44 window[cbid] = undefined;
45 var e = document.getElementById(cbid);
46 e.parentNode.removeChild(e);
47 if (meta.errorDetails) {
48 return;
49 }
50 _this.initMetadata(meta);
51 };
52 var urlScheme = (document.location.protocol === 'file:') ? 'http' : document.location.protocol.slice(0, -1);
53 var url = urlScheme + '://dev.virtualearth.net/REST/v1/Imagery/Metadata/'
54 + this.options.type + '?include=ImageryProviders&jsonp=' + cbid +
55 '&key=' + this._key + '&UriScheme=' + urlScheme;
56 var script = document.createElement('script');
57 script.type = 'text/javascript';
58 script.src = url;
59 script.id = cbid;
60 document.getElementsByTagName('head')[0].appendChild(script);
61 },
62
63 initMetadata: function(meta) {
64 var r = meta.resourceSets[0].resources[0];
65 this.options.subdomains = r.imageUrlSubdomains;
66 this._url = r.imageUrl;
67 this._providers = [];
68 if (r.imageryProviders) {
69 for (var i = 0; i < r.imageryProviders.length; i++) {
70 var p = r.imageryProviders[i];
71 for (var j = 0; j < p.coverageAreas.length; j++) {
72 var c = p.coverageAreas[j];
73 var coverage = {zoomMin: c.zoomMin, zoomMax: c.zoomMax, active: false};
74 var bounds = new L.LatLngBounds(
75 new L.LatLng(c.bbox[0]+0.01, c.bbox[1]+0.01),
76 new L.LatLng(c.bbox[2]-0.01, c.bbox[3]-0.01)
77 );
78 coverage.bounds = bounds;
79 coverage.attrib = p.attribution;
80 this._providers.push(coverage);
81 }
82 }
83 }
84 this._update();
85 },
86
87 _update: function() {
88 if (this._url === null || !this._map) return;
89 this._update_attribution();
90 L.TileLayer.prototype._update.apply(this, []);
91 },
92
93 _update_attribution: function() {
94 var bounds = this._map.getBounds();
95 var zoom = this._map.getZoom();
96 for (var i = 0; i < this._providers.length; i++) {
97 var p = this._providers[i];
98 if ((zoom <= p.zoomMax && zoom >= p.zoomMin) &&
99 bounds.intersects(p.bounds)) {
100 if (!p.active && this._map.attributionControl)
101 this._map.attributionControl.addAttribution(p.attrib);
102 p.active = true;
103 } else {
104 if (p.active && this._map.attributionControl)
105 this._map.attributionControl.removeAttribution(p.attrib);
106 p.active = false;
107 }
108 }
109 },
110
111 onAdd: function(map) {
112 this.loadMetadata();
113 L.TileLayer.prototype.onAdd.apply(this, [map]);
114 },
115
116 onRemove: function(map) {
117 for (var i = 0; i < this._providers.length; i++) {
118 var p = this._providers[i];
119 if (p.active && this._map.attributionControl) {
120 this._map.attributionControl.removeAttribution(p.attrib);
121 p.active = false;
122 }
123 }
124 L.TileLayer.prototype.onRemove.apply(this, [map]);
125 }
126 });
127
128 L.bingLayer = function (key, options) {
129 return new L.BingLayer(key, options);
130 };