/
bing.js
77 lines (61 loc) · 2.46 KB
/
bing.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// namespacing!
if (!com) {
var com = { };
if (!com.modestmaps) {
com.modestmaps = { };
}
}
com.modestmaps.BingProvider = function(key, style, onready) {
this.key = key;
this.style = style;
// hit the imagery metadata service
// http://msdn.microsoft.com/en-us/library/ff701716.aspx
// Aerial, AerialWithLabels, Road
var script = document.createElement('script');
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);
script.src = 'http://dev.virtualearth.net/REST/V1/Imagery/Metadata/'+style+'/?key='+key+'&jsonp=onBingComplete';
function toMicrosoft(column, row, zoom) {
// generate zoom string by interleaving row/col bits
// NB:- this assumes you're only asking for positive row/cols
var quadKey = "";
for (var i = 1; i <= zoom; i++)
{
var rowBit = (row >> zoom-i) & 1;
var colBit = (column >> zoom-i) & 1;
quadKey += (rowBit << 1) + colBit;
}
return quadKey;
}
var provider = this;
window.onBingComplete = function(data) {
var resourceSets = data.resourceSets;
for (var i = 0; i < resourceSets.length; i++) {
var resources = data.resourceSets[i].resources;
for (var j = 0; j < resources.length; j++) {
var resource = resources[j];
var serverSalt = Math.floor(Math.random() * 4);
provider.getTileUrl = function(coord) {
var quadKey = toMicrosoft(coord.column, coord.row, coord.zoom);
// this is so that requests will be consistent in this session, rather than totally random
var server = Math.abs(serverSalt + coord.column + coord.row + coord.zoom) % 4;
return resource.imageUrl
.replace('{quadkey}',quadKey)
.replace('{subdomain}', resource.imageUrlSubdomains[server]);
}
// TODO: use resource.imageWidth
// TODO: use resource.imageHeight
}
}
// TODO: display data.brandLogoUri
// TODO: display data.copyright
onready(provider);
}
}
com.modestmaps.BingProvider.prototype = {
key: null,
style: null,
subdomains: null,
getTileUrl: null
}
com.modestmaps.extend(com.modestmaps.BingProvider, com.modestmaps.MapProvider);