views:

2088

answers:

3

Hi. 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

mymapView = (MyMapView) findViewById(R.id.map);  //results in a classcast exception

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?

Thanks in advance. Bd

+3  A: 

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

public void onLongPress(MotionEvent e, ManagedOverlay overlay)
I82Much
thanks for your reply.. i used the library you mentioned.. i liked using double tap instead of longpress.
vamsibm
I agree; the long press is problematic because it can pop up when you're just scrolling the map.
I82Much
+1  A: 

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;
 }
}
David Almilli
cool.. that's pretty smart.maybe i'll get to try it out sometime, now that I'm done with this project. :)
vamsibm
A: 

can u share with us how u do it??

ambrose