Skip to content

Instantly share code, notes, and snippets.

@DanielGrech
Created May 2, 2012 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielGrech/2572571 to your computer and use it in GitHub Desktop.
Save DanielGrech/2572571 to your computer and use it in GitHub Desktop.
Android Mapview with longpress support
public class LongPressMapView extends MapView {
private boolean mWasLongClick = false;
private LongPressMapView.OnLongpressListener longpressListener;
public LongPressMapView(Context context, String apiKey) {
super(context, apiKey);
}
public LongPressMapView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LongPressMapView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setOnLongpressListener(LongPressMapView.OnLongpressListener listener) {
longpressListener = listener;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mGestureDetector.onTouchEvent(event);
if(mWasLongClick) {
mWasLongClick = false;
return true;
} else {
return super.onTouchEvent(event);
}
}
final GestureDetector mGestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
public void onLongPress(MotionEvent e) {
if(longpressListener != null) {
longpressListener.onLongpress(LongPressMapView.this,
getProjection().fromPixels((int) e.getX(), (int) e.getY()));
}
mWasLongClick = true;
}
});
public static interface OnLongpressListener {
public void onLongpress(MapView view, GeoPoint longpressLocation);
}
}
@johan974
Copy link

johan974 commented Jan 4, 2014

Hi,
Can this code be combined with a ContextMenu?
How could I that make possible?
Hope you can help.
Cheers, J

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment