Subversion Repositories eFlore/Applications.moissonnage

Rev

Details | Last modification | View Log | RSS feed

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