1 L
.BingLayer
= L
.TileLayer
.extend({
3 subdomains
: [0, 1, 2, 3],
9 initialize: function(key
, options
) {
10 L
.Util
.setOptions(this, options
);
14 this.metaRequested
= false;
17 tile2quad: function(x
, y
, z
) {
19 for (var i
= z
; i
> 0; i
--) {
21 var mask
= 1 << (i
- 1);
22 if ((x
& mask
) !== 0) digit
+= 1;
23 if ((y
& mask
) !== 0) digit
+= 2;
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
);
38 loadMetadata: function() {
39 if (this.metaRequested
) return;
40 this.metaRequested
= true;
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
) {
50 _this
.initMetadata(meta
);
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';
60 document
.getElementsByTagName('head')[0].appendChild(script
);
63 initMetadata: function(meta
) {
64 var r
= meta
.resourceSets
[0].resources
[0];
65 this.options
.subdomains
= r
.imageUrlSubdomains
;
66 this._url
= r
.imageUrl
;
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)
78 coverage
.bounds
= bounds
;
79 coverage
.attrib
= p
.attribution
;
80 this._providers
.push(coverage
);
88 if (this._url
=== null || !this._map
) return;
89 this._update_attribution();
90 L
.TileLayer
.prototype._update
.apply(this, []);
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
);
104 if (p
.active
&& this._map
.attributionControl
)
105 this._map
.attributionControl
.removeAttribution(p
.attrib
);
111 onAdd: function(map
) {
113 L
.TileLayer
.prototype.onAdd
.apply(this, [map
]);
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
);
124 L
.TileLayer
.prototype.onRemove
.apply(this, [map
]);
128 L
.bingLayer = function (key
, options
) {
129 return new L
.BingLayer(key
, options
);