Created
December 10, 2013 22:03
-
-
Save dbrockman/7901077 to your computer and use it in GitHub Desktop.
Google maps latlng bounds with center and zoom level in map viewport
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing! I spent a horrible amount of time trying to figure this out by myself :)