On zoom event for google maps on android
Solution 1
With the Google Maps Android API v2 you can use a GoogleMap.OnCameraChangeListener like this:
mMap.setOnCameraChangeListener(new OnCameraChangeListener() {
private float currentZoom = -1;
@Override
public void onCameraChange(CameraPosition pos) {
if (pos.zoom != currentZoom){
currentZoom = pos.zoom;
// do you action here
}
}
});
Solution 2
You can implement your own basic "polling" of the zoom value to detect when the zoom has been changed using the android Handler.
Using the following code for your runnable event. This is where your processing should be done.
private Handler handler = new Handler();
public static final int zoomCheckingDelay = 500; // in ms
private Runnable zoomChecker = new Runnable()
{
public void run()
{
checkMapIcons();
handler.removeCallbacks(zoomChecker); // remove the old callback
handler.postDelayed(zoomChecker, zoomCheckingDelay); // register a new one
}
};
Start a callback event using
protected void onResume()
{
super.onResume();
handler.postDelayed(zoomChecker, zoomCheckingDelay);
}
and stop it when you're leaving the activity using
protected void onPause()
{
super.onPause();
handler.removeCallbacks(zoomChecker); // stop the map from updating
}
Article this is based on can be found here if you want a longer write up.
Solution 3
I use this library which has an onZoom function listener. http://code.google.com/p/mapview-overlay-manager/. Works well.
Solution 4
As the OP said, use the onUserInteractionEvent and do the test yourself.
Solution 5
Here is a clean solution. Simply add this TouchOverlay private class to your activity and a method called onZoom (that is called by this inner class).
Note, you'll have to add this TouchOverlay to your mapView e.g.
mapView.getOverlays().add(new TouchOverlay());
It keeps track of the zoom level whenever the user touches the map e.g. to double-tap or pinch zoom and then fires the onZoom method (with the zoom level) if the zoom level changes.
private class TouchOverlay extends com.google.android.maps.Overlay {
int lastZoomLevel = -1;
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapview) {
if (event.getAction() == 1) {
if (lastZoomLevel == -1)
lastZoomLevel = mapView.getZoomLevel();
if (mapView.getZoomLevel() != lastZoomLevel) {
onZoom(mapView.getZoomLevel());
lastZoomLevel = mapView.getZoomLevel();
}
}
return false;
}
}
public void onZoom(int level) {
reloadMapData(); //act on zoom level change event here
}
Bjarke Freund-Hansen
Updated on July 09, 2022Comments
-
Bjarke Freund-Hansen almost 2 years
We're building an application which is using the google maps api for android.
I have my MapController and MapView, and I enable the built-in zoom controls using:
mapView.setBuiltInZoomControls(true);
I would now like to get an event when the user actually zooms on the map, how do I go about that? I can find no such event or any general event where I could detect a change in zoom level.
Update
The mapView.getZoomControls() is deprecated. And the documentation suggests using mapView.setBuiltInZoomControls(bool) instead. This is okay, but I simply cannot figure out how to act on events from the built in zoom controls.
-
Bjarke Freund-Hansen over 14 yearsThanks for the answer. The MapView.getZoomControls() is deprecated, with the suggestion of using the MapView.setBuiltInZoomControls(boolean) method. But I cannot find anywhere to get any events from these built-in zoom controls. Ref: code.google.com/android/add-ons/google-apis/reference/com/….
-
JasonOfEarth over 14 yearsI see. Well it looks like there is an undocumented mapView.getZoomButtonsController() that you could use, described in code.google.com/p/android/issues/detail?id=3348 otherwise you'll just have to use the mapview.onkeydown and test for the zoom.
-
Bjarke Freund-Hansen over 14 yearsI got what I wanted using the onUserInteraction event, and manually testing for a change in the zoom level. It works really nice with a toast message notifying the user of the necessity of zooming-in to view my overlay.
-
Bahadır Yıldırım about 13 years
onUserInteractionEvent()
is called before the zoom level changes. This effectively means that with each zoom event, the application monitoring the zoom is running one event behind. -
Bjarke Freund-Hansen about 13 years@Paul: Hi. Originally I did not consider this, but it seems you are right, the toast I display are always one even behind.
-
Bahadır Yıldırım about 13 yearsYou can create a
Handler
instance and periodically check for zoom level changes through something likepostDelayed(new Runnable() {...}, 500)
, but I really don't like that solution. -
Bahadır Yıldırım about 13 yearsI've awarded the bounty to this answer, because this is the simplest way of determining whether the map has been moved or zoomed.
-
Bahadır Yıldırım about 13 yearsThis very specifically answers the OP's question, but only raises an event for zoom events through the zoom buttons. Pinching, for instance, does not call the listener's callback.
-
Bahadır Yıldırım about 13 yearsSounds promising. I will take a closer look!
-
Ted Gueniche over 11 yearsWith your solution, I was able to easily capture zoom changes! Thx
-
Aelexe over 11 yearsBrilliant solution, however it was not detecting the first change in zoom. Removing the first if statement for -1 fixed this though.
-
Someone Somewhere over 6 years
setOnCameraChangeListener()
is now deprecated