Get latitude and longitude of the viewport’s sides
Possible Duplicate:
get current latlng bounds?
I’m looking for the best way to use the Google maps API to get the latitude and longitude of the map viewport. Ideally I’d like to be able to use the latitude of the map’s upper and lower borders and the longitude of the sides. The idea is the figure out the area of the visible map and adjust some related elements on the page based on what’s currently visible on the map. Is there a a function in the map’s API that handles this?
Thanks, any help is much appreciated.
Map.getBounds()
gives you the LatLngBounds
of the current viewport. You can then use LatLngBounds.getNorthEast()
and LatLngBounds.getSouthWest()
to get north-east (top right) and south-west (bottom-left) coordinates as LatLng
. More specifically:
var lat0 = map.getBounds().getNorthEast().lat();
var lng0 = map.getBounds().getNorthEast().lng();
var lat1 = map.getBounds().getSouthWest().lat();
var lng1 = map.getBounds().getSouthWest().lng();
Use method getBounds
of the Map class.