开发者

Context menu on a MapView, or other solutions

开发者 https://www.devze.com 2023-02-04 20:49 出处:网络
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开发者_如何学Python.

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开发者_如何学Python. 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;
    }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消