How to differentiate touches on MapView and its Overlay?
I need to have one overlay on the MapView, when user clicks the overlay item(a small pin), I show a balloon callout, but when user clicks on other places on the MapView, it removes the overlay and adds a new one at the touches position. I set OnTouchListener on MapView, so I can detect touches, but how do I differenti开发者_运维问答ate the touch from the ones on overlay?
Thanks!
Well, I supose you use the onTap(int index) method but it fire only when user click the OverlayItem. So, in your case you should combine the onTap() and the onTouchEvent() methods, like this:
@Override
protected boolean onTap(int index) {
selectedItem = items.get(index);
Toast.makeText(context, "BooM!",
Toast.LENGTH_SHORT).show();
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
selectedItem = null;
return false;
}
You can see using debugger that If you tap your item — you are in onTap(), if not — you are in onTouchEvent(). And of course you should override the onDraw() method to draw your balloon.
精彩评论