Skip to content

Instantly share code, notes, and snippets.

@dbrockman
Created December 10, 2013 22:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbrockman/7901077 to your computer and use it in GitHub Desktop.
Save dbrockman/7901077 to your computer and use it in GitHub Desktop.
Google maps latlng bounds with center and zoom level in map viewport
function getBoundsAtLatLngWithZoom(map, center, zoom) {
var p = map.getProjection();
if (!p) {
return null;
}
var el = $(map.getDiv());
var zf = Math.pow(2, zoom) * 2;
var dw = (el.width() | 0) / zf;
var dh = (el.height() | 0) / zf;
var cpx = p.fromLatLngToPoint(center);
return new google.maps.LatLngBounds(
p.fromPointToLatLng(new google.maps.Point(cpx.x - dw, cpx.y + dh)),
p.fromPointToLatLng(new google.maps.Point(cpx.x + dw, cpx.y - dh)));
}
@johannesjo
Copy link

Thanks for sharing! I spent a horrible amount of time trying to figure this out by myself :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment