Google Maps V2 - Android - Get the current zoom level

JavaAndroidGoogle Maps-Android-Api-2Android Maps-V2

Java Problem Overview


How do I get the current zoom level as an integer on a GoogleMap. I need to take this code from GMaps v1.1:

MapView mGoogleMapView; 

int zoomLevel = mGoogleMapView.getZoomLevel();

I am aware of the methods getMinZoomLevel() and getMaxZoomLevel() however I can't find anything in the Android GMap V2 documentation that will give the current zoom level. Does anyone have any pointers on how to do this?

Any help would be appreciated.

Java Solutions


Solution 1 - Java

GoogleMap map;

....

float zoom = map.getCameraPosition().zoom;

Solution 2 - Java

I think OnCameraChangeListener will do the trick..

map.setOnCameraChangeListener(new OnCameraChangeListener() {

    private float currentZoom = -1;

    @Override
    public void onCameraChange(CameraPosition position) {
        if (position.zoom != currentZoom){
            currentZoom = position.zoom;  // here you get zoom level
        }
    }
});

Update:

From Google Play service 9.4.0 OnCameraChangeListener has been deprecated and it will no longer work soon.Alternately they are replaced by OnCameraMoveStarted‌​Listener,OnCameraMoveListener,OnCameraMoveCancel‌​edListener and OnCameraIdleListener.

Hence we can use OnCameraIdleListener here to get camera's current zoom level.

Code Sample:

map.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
    @Override
    public void onCameraIdle() {
        int zoomLevel = map.getCameraPosition().zoom;
        //use zoomLevel value..
    }
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser268397View Question on Stackoverflow
Solution 1 - JavaPavel DudkaView Answer on Stackoverflow
Solution 2 - JavaridoyView Answer on Stackoverflow