MapView 上的上下文菜单或其他解决方案

发布于 2024-10-11 23:59:04 字数 2129 浏览 2 评论 0原文

我需要在我的应用程序中实现 MapView 上的上下文菜单,它获取所选长单击点的坐标并显示一组选项。 我尝试为上下文菜单注册 MapActivity,然后重写 oncreatecontextmenu 方法,但 longclick 事件似乎没有触发。 (例如,我在 oncreatecontextmenu 方法中放入了一些日志,但它从未在 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;
    }

I need to implement in my application a context menu on a MapView, which takes the coordinates of the selected long clicked point and show a set of option.
I tried to register the MapActivity for context menu and then overriding the oncreatecontextmenu method, but the longclick event seems not to fire. (e.g. I put some log in the oncreatecontextmenu method which never show on ddms)

I searched on the web and in the Android documentation but I have found nothing satisfying.
So, is it really impossible to create a contextmenu on a MapView?
If so, is there a way to implement something similar?
I'm posting the (simple) code:

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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话墙 2024-10-18 23:59:04

由于长单击事件不会触发,请尝试以下操作。

编写您自己的 Overlay 类并重写 onTab() 方法。在该方法中,您调用 openContextMenu() 方法。当您在地图上单击选项卡时,这应该会打开上下文菜单。

As the long click event doesn't fire try the following.

Write you own Overlay class and override the onTab() method. Within the method you call the openContextMenu() method. This should open the context menu when you tab on the map.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文