我需要在我的应用程序中实现MapView上的上下文菜单,该菜单获取所选长按点的坐标并显示一组选项。 我尝试将MapActivity注册到上下文菜单,然后重写oncreatecontextmenu方法,但是longclick事件似乎没有触发。 (例如,我在ondatecontextmenu方法中放入了一些从未在ddms上显示的方法)
我在网络和Android文档中搜索过,但我发现没什么令人满意的。 那么,在MapView上创建上下文菜单真的不可能吗? 如果是这样,有没有办法实现类似的东西? 我发布了(简单)代码:
public class ChoosePosition extends MapActivity {
MapView mappa;
MapController mapCtr;
LocationManager locManager;
LocationListener locLstn;
Location myLastLoc;
String locProvider;
double mylat, mylongi;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chooseposition);
locManager = (LocationManager)getSystemService(LOCATION_SERVICE);
locLstn = new MyLocationListener();
locManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
2000, 10,
locLstn);
mappa = (MapView) findViewById(R.id.map2);
mappa.setLongClickable(true);
mapCtr = mappa.getController();
// attiviamo lo zoom integrato
mappa.setBuiltInZoomControls(true);
// getting last known location
myLastLoc = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
GeoPoint point;
if (myLastLoc == null) {
mylat = myLastLoc.getLatitude();
mylongi = myLastLoc.getLongitude();
// trasformiamo l'ultima posizione in un GeoPoint
point= new GeoPoint((int) (myLastLoc.getLatitude() * 1E6),
(int) (myLastLoc.getLongitude() * 1E6));
}
// setting center and zoom
mapCtr.setCenter(point);
mapCtr.setZoom(17);
// adding an overlay
MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
mappa.getOverlays().add(myLocationOverlay);
registerForContextMenu(mappa);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
Log.i("MYINFO", "I'm in");
menu.add(Menu.NONE, 0, Menu.NONE, "First option");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
return true;
}
答案 0 :(得分:3)
由于长按事件不会触发,请尝试以下操作。
编写自己的Overlay类并覆盖onTab()方法。在方法中,您可以调用openContextMenu()方法。当您在地图上选项卡时,这应该打开上下文菜单。