Created
May 2, 2012 00:19
-
-
Save DanielGrech/2572571 to your computer and use it in GitHub Desktop.
Android Mapview with longpress support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Can this code be combined with a ContextMenu?
How could I that make possible?
Hope you can help.
Cheers, J