I have a mapview and i wish to display a ContextMenu when longclick but so far the closest solution i've found is here on anddev , the main reason i do not like that method is because any click activate the ContextMenu instead of a long click.
Question:
Is there a way to display the ContextMenu of a Map without using Overlays? Why?
This is the approach I used. I created an AbstractMap class which extended MapActivity. From here I then extended from the AbstractMap class to create a Map which suited my requirements. Note I only used this approach since I was requiring several maps with varying properties for my application. You could simply remove the Abstract keyword from the AbstractMap, directly override the OnGestureListener methods within this class and instantiate it.
Here is the AbstractMap class
public abstract class AbstractMap extends MapActivity implements OnGestureListener, OnDoubleTapListener {
public MapView mapView;
public MapController mapController;
public List<Overlay> mapOverlays;
private GestureDetector detector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.mapp);
detector = new GestureDetector(this, this);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setOnTouchListener(otl);
mapController = mapView.getController();
mapOverlays = mapView.getOverlays();
} catch (Exception e) {
Log.e("Error", "Exception", e);
}
}
public OnTouchListener otl = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (detector.onTouchEvent(event))
return true;
else
return false;
}
};
}
Here is the LongPressMap
public class LongPressMap extends AbstractMap {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public boolean onDown(MotionEvent event) {
return false;
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
//Log.d("Debug","On Scrtoll");
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
Hope this helps.
It might worth a shot to try writing an onKeyLongPress method for the mapView. To do this, you'll have to make another class that extends MapView and then use the new class in place of your MapView. Just have a default constructor that defers to the super constructor. Eclipse will create this automatically for you if you let it generate superclass constructors.
From there, override onKeyLongPress, have it call whatever methods you normally would to display the menu.
onKeyLongPress means exactly that. A KEY was held down. What you want to do is implement a GestureListener. Here's an example on how to get that working:
GestureListener Example
Of course all of the information you get is going to be in pixels, so you'll need to project that into your map space to get lat long.