[PLUGINS] +clavettes et dependances
[lhc/web/clavette_www.git] / www / plugins / gis / lib / leaflet / plugins / Bing.js
1 /* global console: true */
2 L.BingLayer = L.TileLayer.extend({
3 options: {
4 subdomains: [0, 1, 2, 3],
5 type: 'Aerial',
6 attribution: 'Bing',
7 culture: ''
8 },
9
10 initialize: function(key, options) {
11 L.Util.setOptions(this, options);
12
13 this._key = key;
14 this._url = null;
15 this.meta = {};
16 this.loadMetadata();
17 },
18
19 tile2quad: function(x, y, z) {
20 var quad = '';
21 for (var i = z; i > 0; i--) {
22 var digit = 0;
23 var mask = 1 << (i - 1);
24 if ((x & mask) !== 0) digit += 1;
25 if ((y & mask) !== 0) digit += 2;
26 quad = quad + digit;
27 }
28 return quad;
29 },
30
31 getTileUrl: function(p, z) {
32 var zoom = this._getZoomForUrl();
33 var subdomains = this.options.subdomains,
34 s = this.options.subdomains[Math.abs((p.x + p.y) % subdomains.length)];
35 return this._url.replace('{subdomain}', s)
36 .replace('{quadkey}', this.tile2quad(p.x, p.y, zoom))
37 .replace('{culture}', this.options.culture);
38 },
39
40 loadMetadata: function() {
41 var _this = this;
42 var cbid = '_bing_metadata_' + L.Util.stamp(this);
43 window[cbid] = function (meta) {
44 _this.meta = meta;
45 window[cbid] = undefined;
46 var e = document.getElementById(cbid);
47 e.parentNode.removeChild(e);
48 if (meta.errorDetails) {
49 if (window.console) console.log('Leaflet Bing Plugin Error - Got metadata: ' + meta.errorDetails);
50 return;
51 }
52 _this.initMetadata();
53 };
54 var url = document.location.protocol + '//dev.virtualearth.net/REST/v1/Imagery/Metadata/' + this.options.type + '?include=ImageryProviders&jsonp=' + cbid +
55 '&key=' + this._key + '&UriScheme=' + document.location.protocol.slice(0, -1);
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() {
64 var r = this.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 onRemove: function(map) {
112 for (var i = 0; i < this._providers.length; i++) {
113 var p = this._providers[i];
114 if (p.active && this._map.attributionControl) {
115 this._map.attributionControl.removeAttribution(p.attrib);
116 p.active = false;
117 }
118 }
119 L.TileLayer.prototype.onRemove.apply(this, [map]);
120 }
121 });
122
123 L.bingLayer = function (key, options) {
124 return new L.BingLayer(key, options);
125 };