Double click/tap detection on android’s MapView
If there is a cleaner way to do it, please share
- Override the default MapView with your own implementation;
- Override the onInterceptTouchEvent method;
- Check if the last event was also a click and happened close by (say in the last 250ms);
- If so, it’s a double tap; do whatever you want (in this case I zoom in on the last clicked point);
- If not, ignore
Here’s the code (Sorry for the formatting mess, but…
):
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import com.google.android.maps.MapView;
public class MyMapView extends MapView {
private long lastTouchTime = -1;
public MyMapView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
long thisTime = System.currentTimeMillis();
if (thisTime - lastTouchTime < 250) {
// Double tap
this.getController().zoomInFixing((int) ev.getX(), (int) ev.getY());
lastTouchTime = -1;
} else {
// Too slow
lastTouchTime = thisTime;
}
}
return super.onInterceptTouchEvent(ev);
}
}
Related:
- Double click/tap detection on android’s MapView If there is a cleaner way to do it,...
- Android location provider mock So, yeah, I resumed playing around with android, this time...
Categorised as: computers, software development, tips
This solution has problem with zoom buttons in mapView! When you quickly zoom with buttons, it do bad moves.
This solution is a bit old. I'm sure that there are better ones these days
<p style=”color: #A0A0A8;”>
Really simple and clean solution, thanks for sharing!
I would like to say thanks!
I’m a student and currently learning stuff about programming, and the way you just recreated the mapview just made me go “cooooooool”
keep up the good work
Thank you,
Just what I was looking for.
I modified this slightly as I wanted to know the coords of the location the user had clicked so I could add another item to the map. So instead of getting the controller and zooming I have: getProjection().fromPixels((int)ev.getX(), (int)ev.getY());
At that point I launch the context menu allowing the user to insert an item at that point or cancel.
Regards
Victoria
No problem, glad I could help