Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zoom level listener in google maps v2 in android

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.

like image 789
Khikmat Avatar asked Dec 20 '12 14:12

Khikmat


People also ask

How do I check the zoom level on Google Maps?

You can change the zoom level of the map using simple steps. Step 1 Go to Add or Edit Map page . Step 2 Select 'Default zoom level' in the 'Map Information section'. Step 3 click save map and see the changes.

How do I change the zoom level on Google Maps?

Users can zoom the map by clicking the zoom controls. They can also zoom and pan by using two-finger movements on the map for touchscreen devices.

How do you zoom in closer on Google Maps?

First, find a spot on Google Maps and zoom in as far as the "+" button above the magnification slider on the left edge of the map allows. Be sure to stop short of going into Street View, if the location you're gazing at supports it.


2 Answers

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.

like image 140
DiscDev Avatar answered Sep 21 '22 09:09

DiscDev


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);         }     } }); 
like image 44
ᴛʜᴇᴘᴀᴛᴇʟ Avatar answered Sep 22 '22 09:09

ᴛʜᴇᴘᴀᴛᴇʟ