Google Maps V2 - Android - Get the current zoom level
Asked Answered
I

2

78

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.

Isogamete answered 3/5, 2013 at 19:48 Comment(1)
Apparently this api doesn't exist in current Google Maps....Arlinda
L
235
GoogleMap map;

....

float zoom = map.getCameraPosition().zoom;
Loeffler answered 3/5, 2013 at 19:51 Comment(1)
This has to be done on UI thread, or you'll get an exception!Arlinda
I
47

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..
    }
});
Incisor answered 3/5, 2013 at 20:14 Comment(4)
This method was deprecated. Replaced by setOnCameraMoveStartedListener(GoogleMap.OnCameraMoveStartedListener), setOnCameraMoveListener(GoogleMap.OnCameraMoveListener), setOnCameraMoveCanceledListener(GoogleMap.OnCameraMoveCanceledListener) and setOnCameraIdleListener(GoogleMap.OnCameraIdleListener).Combe
@NaszNjokaSr., thanks for your concern. Updated accordingly.Incisor
Isn't it possible in your updated version, just to use map.getCameraPosition().zoom ? Why is the implementation of a listener necessary? And with this onCameraIdleListener wouldn't you get the current zoom level only when the camera goes into the idle mode?Maryannmaryanna
and looks like zoom value is a float not an int.Glaser

© 2022 - 2024 — McMap. All rights reserved.