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:
I think there might another answer to this question. The Overlay class draw method is called after any zoom and I believe this is called after the zoom level changes. Could you not architect your app to take advantage of this? You could even use a dummy overlay just to detect this if you wanted to. Would this not be more efficient than using a runnable with a delay?
@Override
public boolean draw (Canvas canvas, MapView mapView, boolean shadow, long when) {
int zoomLevel = mapView.getZoomLevel();
// do what you want with the zoom level
return super.draw(canvas, mapView, shadow, when);
}
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
}
}
});
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
}
The android API suggests you use ZoomControls which has a setOnZoomInClickListener()
To add these zoom controls you would:
ZoomControls mZoom = (ZoomControls) mapView.getZoomControls();
And then add mZoom to your layout.
You Can use
ZoomButtonsController zoomButton = mapView.getZoomButtonsController();
zoomButton.setOnZoomListener(listener);
hope it helps
As the OP said, use the onUserInteractionEvent and do the test yourself.