android maps: How to Long Click a Map?

vamsibm picture vamsibm · Nov 5, 2009 · Viewed 25.3k times · Source

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?

Answer

David Almilli picture David Almilli · Jun 7, 2010

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.

    List<Overlay> overlays = mapView.getOverlays();
    overlays.clear();
    overlays.add(new MapGestureDetectorOverlay(new MyOnGestureListener()));

And here's the class:

public class MapGestureDetectorOverlay extends Overlay implements OnGestureListener {
 private GestureDetector gestureDetector;
 private OnGestureListener onGestureListener;

 public MapGestureDetectorOverlay() {
  gestureDetector = new GestureDetector(this);
 }

 public MapGestureDetectorOverlay(OnGestureListener onGestureListener) {
  this();
  setOnGestureListener(onGestureListener);
 }

 @Override
 public boolean onTouchEvent(MotionEvent event, MapView mapView) {
  if (gestureDetector.onTouchEvent(event)) {
   return true;
  }
  return false;
 }

 @Override
 public boolean onDown(MotionEvent e) {
  if (onGestureListener != null) {
   return onGestureListener.onDown(e);
  }
  return false;
 }

 @Override
 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
   float velocityY) {
  if (onGestureListener != null) {
   return onGestureListener.onFling(e1, e2, velocityX, velocityY);
  }
  return false;
 }

 @Override
 public void onLongPress(MotionEvent e) {
  if (onGestureListener != null) {
   onGestureListener.onLongPress(e);
  }
 }

 @Override
 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
   float distanceY) {
  if (onGestureListener != null) {
   onGestureListener.onScroll(e1, e2, distanceX, distanceY);
  }
  return false;
 }

 @Override
 public void onShowPress(MotionEvent e) {
  if (onGestureListener != null) {
   onGestureListener.onShowPress(e);
  }
 }

 @Override
 public boolean onSingleTapUp(MotionEvent e) {
  if (onGestureListener != null) {
   onGestureListener.onSingleTapUp(e);
  }
  return false;
 }

 public boolean isLongpressEnabled() {
  return gestureDetector.isLongpressEnabled();
 }

 public void setIsLongpressEnabled(boolean isLongpressEnabled) {
  gestureDetector.setIsLongpressEnabled(isLongpressEnabled);
 }

 public OnGestureListener getOnGestureListener() {
  return onGestureListener;
 }

 public void setOnGestureListener(OnGestureListener onGestureListener) {
  this.onGestureListener = onGestureListener;
 }
}