How can i set a listener on an transparent ImageView that is above the map, to only react to pinching (zooming) events on it, while everything else like moving the map gets handled by the Google map?
My goal is to create centered zooming on the map which can be done like so:
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mGoogleMap.getCameraPosition().target, amountOfZoomNeeded));
but i need to catch that pinch event first, and only that event since onTouch blocks using the map below.
EDIT:
I managed to create centered zooming using the ScaleGestureDetectors onScale() method:
public class PinchListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
private GoogleMap googleMap;
public PinchListener(GoogleMap googleMap){
this.googleMap = googleMap;
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
double zoom = googleMap.getCameraPosition().zoom;
zoom = zoom + Math.log(detector.getScaleFactor()) / Math.log(1.5d);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(googleMap.getCameraPosition().target, (float) zoom);
googleMap.moveCamera(update);
return true;
}
}
And in my MainActivity i initialize it, where mMapWrapper is the transparent ImageView wrapper i mentioned earlier.
ScaleGestureDetector mScaleGestureDetector = new ScaleGestureDetector(this, new PinchListener(mGoogleMap));
mMapWrapper.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
mScaleGestureDetector.onTouchEvent(event);
return true;
}
});
But now I'm facing a problem where I can't move the map since all touch events are getting eaten by the onTouch() method of the wrapper. How can i prevent this from happening?
So this is how i managed to do this.
I created two classes for detecting certain events, like a fling, double tap, scroll(drag) and scale(zoom):
I then initialized a GestureDetector and ScaleGestureDetector in my MainActivity:
I had already created a transparent image wrapper in my activity_main to detect all these events and then simulate them on the Google Map below, like this:
And then finally in my MainActivity i created an OnTouchListener for the wrapper:
In the last step i "feed" the right detector the events that i want. If i had not done that, the zooming would not be completely centered since sometimes onScroll(Drag) events would be called and that would move the map.