zoom level listener in google maps v2 in android
Asked Answered
H

4

46

I'm developing an Android app that is supposed to use Google Maps v2. Now i'm stuck at finding when zoom level of map has changed. Can anyone help me?Thanks in advance.

Hendecasyllable answered 20/12, 2012 at 14:4 Comment(0)
T
37

Create an implementation of OnCameraChangeListener, and pass an instance of it to setOnCameraChangeListener() of your GoogleMap. Your listener should be called with onCameraChange() whenever the user changes the zoom, center, or tilt. You find out the new zoom level from the CameraPosition object that you are passed.

Tinney answered 20/12, 2012 at 14:10 Comment(3)
setOnCameraChangeListener is now deprecatedSwellfish
@Tinney what if I want to detect only zoom onDoubleTap on google map??Britishism
@NirmalPrajapat: I have no idea, sorry.Tinney
U
54

If you're looking for how to determine if the zoom level has changed from the previous zoom level, here's what I'd suggest:

Define an instance variable to keep track of the previous zoom level:

//Initialize to a non-valid zoom value
private float previousZoomLevel = -1.0f;

Also, define an instance variable to let you know if the map is zooming:

private boolean isZooming = false;

When you setup your GoogleMap instance, give it an OnCameraChangeListener...

//mMap is an instance of GoogleMap
mMap.setOnCameraChangeListener(getCameraChangeListener());

Now, define the OnCameraChangeListener that will determine if the zoom level has changed:

public OnCameraChangeListener getCameraChangeListener()
{
    return new OnCameraChangeListener() 
    {
        @Override
        public void onCameraChange(CameraPosition position) 
        {
            Log.d("Zoom", "Zoom: " + position.zoom);

            if(previousZoomLevel != position.zoom)
            {
                isZooming = true;
            }

            previousZoomLevel = position.zoom;
        }
    };
}

Now, you can check the value of isZooming to know if you are changing zoom levels.

Make sure to set

isZooming = false;

after you've completed whatever action relies on knowing if the map is zooming.

Ury answered 20/12, 2012 at 16:7 Comment(3)
Also works perfectly as a drag listener. Exactly what I was looking for to filter markers view by the distance to the map center. Thanks!Algophobia
deprecated now.Hadwyn
As is deprecated, use onCameraMoveStarted insteadGelman
D
42

Since previous answers are based on OnCameraChangeListener and that is deprecated, this answer is based on camera OnCameraMoveListener.

In this example, I am changing my Map Type when user changes the zoom (using controls or fingers and zooming).

If zoom level changes to above 18.0, map type changes to MAP_TYPE_HYBRIB and
If zoom level changes to below 18.0, map type changes to MAP_TYPE_NORMAL.

googleMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
    @Override
    public void onCameraMove() {
        CameraPosition cameraPosition = googleMap.getCameraPosition();
        if(cameraPosition.zoom > 18.0) {
            googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        } else {
            googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        }
    }
});
Danitadaniyal answered 12/11, 2016 at 18:35 Comment(3)
Nice! Much cleaner than OnCameraChangeListener. Interesting idea to change the map type.Fishhook
note: this callback is triggered quite a few times for each zoom eventDisplay
@SomeoneSomewhere Yes, it is. While user is zooming in, each "phase" of the zoom will have a trigger. The slower you zoom in/out, the more times it will trigger the listener. The faster you zoom in/out, the less times it will trigger.Dort
T
37

Create an implementation of OnCameraChangeListener, and pass an instance of it to setOnCameraChangeListener() of your GoogleMap. Your listener should be called with onCameraChange() whenever the user changes the zoom, center, or tilt. You find out the new zoom level from the CameraPosition object that you are passed.

Tinney answered 20/12, 2012 at 14:10 Comment(3)
setOnCameraChangeListener is now deprecatedSwellfish
@Tinney what if I want to detect only zoom onDoubleTap on google map??Britishism
@NirmalPrajapat: I have no idea, sorry.Tinney
W
1

You can also override GoogleMap.OnCameraMoveStartedListener and register it with a GogleMap.setOnCameraMoveStartedListener method:

googleMap.setOnCameraMoveStartedListener { reason ->
   when (reason) {
      GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE -> {
         //handle this state
      }
    }
}
Workmanlike answered 7/7, 2021 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.