-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBing.IODB.js
80 lines (74 loc) · 2.94 KB
/
Bing.IODB.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
78
79
80
// Written by Ilya Zverev, licensed WTFPL
L.BingLayer = L.BingLayer.extend({
options: {
offsetServer: 'http://offsets.textual.ru/',
offsetUpdateDistance: 1000,
offsetEnabled: true
},
setOffsetEnabled: function(e) {
if( e != this.options.offsetEnabled ) {
this.options.offsetEnabled = e;
this._updateContainerOffset();
}
},
_update: function() {
if (this._url == null || !this._map) return;
this._update_attribution();
if( !this._offset0 ) this._offset0 = new L.Point(0, 0);
this._refreshOffset();
this._updateContainerOffset();
L.TileLayer.prototype._update.apply(this, []);
},
_refreshOffset: function() {
if( this._map.getZoom() < 15 )
return;
var currentPos = this._map.getCenter();
if( this._lastOffsetPos && this._lastOffsetPos.distanceTo(currentPos) < this.options.offsetUpdateDistance )
return;
this._lastOffsetPos = currentPos;
this._queryIODB(currentPos, this, function(ctx, data) {
var found = null;
for( var i = 0; i < data.length; i++ ) {
if( data[i]['type'] == 'offset' && !data[i]['deprecated'] ) {
found = data[i];
break;
}
}
var distance = 0;
if( found ) {
var from = new L.LatLng(found['lat'], found['lon']);
var to = new L.LatLng(found['imlat'], found['imlon']);
distance = from.distanceTo(to);
ctx._offset = ctx._map.project(to, 22).subtract(ctx._map.project(from, 22));
} else
ctx._offset = ctx._offset0;
ctx._updateContainerOffset();
ctx.fire('offset', { distance: distance, found: found });
});
},
_updateContainerOffset: function() {
if( !this._offset ) return;
var style = this._container.style,
zoom = this._map.getZoom(),
offset = zoom < 15 || !this.options.offsetEnabled ? this._offset0 : this._offset,
zoomDiff = Math.pow(2, 22 - zoom);
style.left = Math.round(-offset.x / zoomDiff) + 'px';
style.top = Math.round(-offset.y / zoomDiff) + 'px';
},
_queryIODB: function (latlng, context, callback) {
var url = this.options.offsetServer + 'get?format=json&imagery=bing&lat=' + latlng.lat + '&lon=' + latlng.lng;
var http = null;
if (window.XMLHttpRequest) {
http = new XMLHttpRequest();
} else if (window.ActiveXObject) { // Older IE.
http = new ActiveXObject("MSXML2.XMLHTTP.3.0");
}
http.onreadystatechange = function() {
if( http.readyState != 4 || http.status != 200 ) return;
var result = eval(http.responseText);
callback(context, result);
};
http.open('GET', url, true);
http.send(null);
}
});