Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get Paddinged Bounds for MapGoogle Maps API
if (google && google.maps && !google.maps.Map.prototype.getPadBounds) {
google.maps.Map.prototype.getPadBounds = function (top, right, bottom, left) {
top = (('' + top).match(/^[0-9]+$/i) ? parseInt(top) : 0);
right = (('' + right).match(/^[0-9]+$/i) ? parseInt(right) : 0);
bottom = (('' + bottom).match(/^[0-9]+$/i) ? parseInt(bottom) : 0);
left = (('' + left).match(/^[0-9]+$/i) ? parseInt(left) : 0);
var bounds = this.getBounds();
var scale = Math.pow(2, this.getZoom());
var proj = this.getProjection();
var sw = proj.fromLatLngToPoint(bounds.getSouthWest());
var ne = proj.fromLatLngToPoint(bounds.getNorthEast());
sw = new google.maps.Point(
((sw.x * scale) + left) / scale,
((sw.y * scale) - bottom) / scale
);
ne = new google.maps.Point(
((ne.x * scale) - right) / scale,
((ne.y * scale) + top) / scale
);
var rect = new google.maps.LatLngBounds(proj.fromPointToLatLng(sw), proj.fromPointToLatLng(ne));
// // Debug: show rectangle
// new google.maps.Rectangle({
// bounds: rect,
// map: this
// });
return rect;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.