Skip to content

Instantly share code, notes, and snippets.

@kijtra
Last active June 25, 2016 05:29
Show Gist options
  • Save kijtra/67ec4c0f0ea006c6d794698b19168127 to your computer and use it in GitHub Desktop.
Save kijtra/67ec4c0f0ea006c6d794698b19168127 to your computer and use it in GitHub Desktop.
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