Google Maps API v2: LatLngBounds from CameraPosition

Garcon picture Garcon · Dec 18, 2012 · Viewed 30.5k times · Source

Is there a simple way to get the LatLngBounds of the visible map from a CameraPosition with Android Google Maps API v2 so that I can use the OnCameraChangeListener to go fetch new data for the markers.

mMap.setOnCameraChangeListener(new OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition position) {
                LatLngBounds bounds = ?;
                fetchNewData(bounds);
            }
        });

Answer

Garcon picture Garcon · Dec 18, 2012

You can't get the LatLngBounds from the CameraPosition, but you can get them easily from the GoogleMap.

private GoogleMap mMap;

mMap.setOnCameraChangeListener(new OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition position) {
                LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;
                fetchData(bounds);
            }
        });