1 /* global console: true */
2 L
.BingLayer
= L
.TileLayer
.extend({
4 subdomains
: [0, 1, 2, 3],
10 initialize: function(key
, options
) {
11 L
.Util
.setOptions(this, options
);
19 tile2quad: function(x
, y
, z
) {
21 for (var i
= z
; i
> 0; i
--) {
23 var mask
= 1 << (i
- 1);
24 if ((x
& mask
) !== 0) digit
+= 1;
25 if ((y
& mask
) !== 0) digit
+= 2;
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
);
40 loadMetadata: function() {
42 var cbid
= '_bing_metadata_' + L
.Util
.stamp(this);
43 window
[cbid
] = function (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
);
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';
60 document
.getElementsByTagName('head')[0].appendChild(script
);
63 initMetadata: function() {
64 var r
= this.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 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
);
119 L
.TileLayer
.prototype.onRemove
.apply(this, [map
]);
123 L
.bingLayer = function (key
, options
) {
124 return new L
.BingLayer(key
, options
);