How do I long click on a mapview so that a place marker appears at that point on the map?
I tried a couple ways without success:
1) Using setOnLongClickListener
on the MapvView
which never detected the longclicks.
2) My other idea was to extend MapView
to override dispatchTouchEvent
.. Create a GestureDetector to respond to longpress callback. But I was stuck midway here as I could not get a handle to my subclassed Mapview. i.e.
MyMapview mymapview; //MyMapView extends MapView
//results in a classcast exception
mymapView = (MyMapView) findViewById(R.id.map);
3) The only other way I know how to try this is:
Detect a MotionEvent.ACTION_DOWN
and post a delayed runnable to a handler and detect longpress if the two other events: acton_move or an action_up, have not happened.
Can someone provide thoughts on any of these methods to detect long presses?
This mapview-overlay-manager library is super. The reason onLongPress gets triggered when 'scrolling' is because the library does not take into account multitouch. You can work around this by rejecting onLongPress if there is more than one pointer involved as so:
Best way I know to do this is to use the open source mapview-overlay-manager and use its gesture listener which provides a callback for
I also got a ClassCastException when I tried using MyMapView. As a workaround, I created a MyMapView object instead of a MapView object and programmatically added it to the layout, and that worked great.
I've found an even easier way. Just make an overlay as the first overlay in the list that does not draw anything and use it to recognize gestures using the GestureDetector. It should then return true if it handled the event so it doesn't get propagated.
And here's the class:
This works with your MapActivity and will allow you to:
This solution is based on Roger Kristiansen's solution. I added x and y point detection so that scrolling is not seen as a long press. This solution is not as elegant as Roger's because I use class variables and put the code in my existing MapActivity instead of extending MapView and creating a listener like he did. But if you want to stick with his code but have better non-multi-touch support, just take the x and y point stuff out of mine and add it to his.
Class variables set at the top of my MapActivity:
Add this function in your MapActivity:
If you're going with the delayed post/message to a handler (a solution I use myself), it can be useful to test if the map has moved, i.e. if
mapView.getMapCenter()
,mapView.getLatitudeSpan()
andmapView.getLongitudeSpan()
return the same as when the pointer went down.