-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfitBoundsPadding.js
More file actions
73 lines (63 loc) · 2 KB
/
fitBoundsPadding.js
File metadata and controls
73 lines (63 loc) · 2 KB
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
(function() {
var oldFitBounds = google.maps.Map.prototype.fitBounds;
google.maps.Map.prototype.fitBounds = function(bounds, opts) {
if(opts) {
var map = this,
scale = Math.pow(2,this.getZoom());
/* Helper methods */
var _convertLatLngToPixel = function(latlng) {
var proj = map.getProjection();
var point = proj.fromLatLngToPoint(latlng);
return {
x: point.x * scale,
y: point.y * scale
}
}
var _convertPixelToLatLng = function(pixel) {
var proj = map.getProjection();
var point = new google.maps.Point(pixel.x / scale, pixel.y / scale);
return proj.fromPointToLatLng(point);
}
var _getPixelBounds = function(bounds, cb) {
if(map.getProjection()) {
var returnVal = {
sw: _convertLatLngToPixel(bounds.getSouthWest()),
ne: _convertLatLngToPixel(bounds.getNorthEast())
}
cb(returnVal);
}
else {
google.maps.event.addListener(map, 'projection_changed', function () {
_getPixelBounds(bounds, cb);
});
}
}
var _extendBoundsByPaddingValue = function(bounds, opts) {
_getPixelBounds(bounds, function(pxbounds) {
for(var prop in opts) {
switch(prop) {
case 'left':
pxbounds.sw.x -= opts.left;
break;
case 'top':
pxbounds.ne.y -= opts.top;
break;
case 'right':
pxbounds.ne.x += opts.right;
break;
case 'bottom':
pxbounds.sw.y += opts.bottom;
break;
}
}
var bounds = new google.maps.LatLngBounds(_convertPixelToLatLng(pxbounds.sw), _convertPixelToLatLng(pxbounds.ne));
oldFitBounds.call(map, bounds);
});
}
_extendBoundsByPaddingValue(bounds, opts);
}
else {
oldFitBounds.call(this, bounds);
}
}
})();