Issue
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?
Solution
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.
Answered By - Bear
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.