Garcon Garcon - 4 months ago 30
Android Question

Google Maps API v2: LatLngBounds from CameraPosition

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

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);
            }
        });