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.

user268397
  • 1,917
  • 7
  • 38
  • 56

2 Answers2

235
GoogleMap map;

....

float zoom = map.getCameraPosition().zoom;
Pavel Dudka
  • 20,754
  • 7
  • 70
  • 83
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..
    }
});
ridoy
  • 6,274
  • 2
  • 29
  • 60
  • 3
    This method was deprecated. Replaced by setOnCameraMoveStartedListener(GoogleMap.OnCameraMoveStartedListener), setOnCameraMoveListener(GoogleMap.OnCameraMoveListener), setOnCameraMoveCanceledListener(GoogleMap.OnCameraMoveCanceledListener) and setOnCameraIdleListener(GoogleMap.OnCameraIdleListener). – Nasz Njoka Sr. Dec 20 '16 at 15:45
  • @NaszNjokaSr., thanks for your concern. Updated accordingly. – ridoy Feb 03 '17 at 06:23
  • 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? – Sedat Kilinc Aug 31 '17 at 18:07
  • and looks like zoom value is a float not an int. – tekin beyaz Jan 10 '19 at 20:35