android – How to get current GoogleMap zoom level?-ThrowExceptions

Exception or error:

On GoogleMap android API v2, how do I get the current map zoom level? On API v1, there used to be float MapView.getZoomLevel(), but there appears to be nothing similar on the API version I am using, and that Google recommends.

I’ve thought of using a class variable to save the zoom level manually via a zoom button click listener, but this doesn’t solve the problem for pinch-type zooming.

Why do I need the current zoom level? I am restricting the map range to a certain rectangle and want any moves that would otherwise leave this rectangle, bounce back. This animation requires me to use a zoom level, for without it, the default maximum zoom level is used. What I really want is to maintain the zoom level used before the move attempt.

protected void recenterMap() {
    map.getMinZoomLevel();
    if(MAP_BOUNDS.contains(map.getCameraPosition().target) == false) {
        CameraPosition cameraPosition = new CameraPosition.Builder()
        .target(MAP_CENTER)      
        .zoom(current_zoom)
        .build();     

        map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    } else {
        MAP_CENTER = map.getCameraPosition().target;
    }
}

Notice that the variable current_zoom is what I need to define.

How to solve:

For Android, try getting the current CameraPosition, and getting the zoom from that.
I believe it’s:

map.getCameraPosition().zoom

###

It should be (on v2 API)

map.getZoom(); 

If not, try document.getElementById("zoom").innerHTML

Leave a Reply

Your email address will not be published. Required fields are marked *