我如何才能使弹出菜单出现后,一个pin添加到谷歌地图android



这是我的主要活动类:

public class MyActivity extends MapActivity implements LocationListener, OnClickListener{
private MapView mapView;
private MyItemizedOverlay itemizedOverlay;
Button route;
Drawable drawableDot;
static GeoPoint src;
static GeoPoint des;
Canvas canvas;
Paint paint;
boolean shadow;
private LocationManager locManager;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    route = (Button) findViewById(R.id.cmd_submit);
    route.setOnClickListener(this);

    //fetch the map view from the layout
    mapView = (MapView) findViewById(R.id.myMapView);
    //make available zoom controls
    mapView.setBuiltInZoomControls(true);
    //latitude and longitude of Rome
    double lat = 41.889882;
    double lon = 12.479267;
    //create geo point
    GeoPoint point = new GeoPoint((int)(lat * 1E6), (int)(lon *1E6));
    //get the MapController object
    MapController controller = mapView.getController();
    //animate to the desired point
    controller.animateTo(point);
    //set the map zoom to 13
    // zoom 1 is top world view
    controller.setZoom(13);
    //invalidate the map in order to show changes
    mapView.invalidate();
    // Use the location manager through GPS
    locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
            0, this);
    //get the current location (last known location) from the location manager
    Location location = locManager
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);

        //if location found display as a toast the current latitude and longitude
    if (location != null) {
        Toast.makeText(
                this,
                "Current location:nLatitude: " + location.getLatitude()
                        + "n" + "Longitude: " + location.getLongitude(),
                Toast.LENGTH_LONG).show();
        point = new GeoPoint((int)(location.getLatitude()*1E6),(int)(location.getLongitude() *1E6));
        controller.animateTo(point);
    } else {
        Toast.makeText(this, "Cannot fetch current location!",
                Toast.LENGTH_LONG).show();
    }
    //when the current location is found – stop listening for updates (preserves battery)
    locManager.removeUpdates(this);
    // fetch the drawable - the pin that will be displayed on the map
    Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
    // create and add an OverlayItem to the MyItemizedOverlay list
    OverlayItem overlayItem = new OverlayItem(point, "", "");
    itemizedOverlay = new MyItemizedOverlay(drawable,this);
    itemizedOverlay.setGestureDetector(new GestureDetector(new MyGestureDetector()));
    itemizedOverlay.addOverlay(overlayItem);

    // add the overlays to the map
    mapView.getOverlays().add(itemizedOverlay);
    mapView.invalidate();
    //when the current location is found – stop listening for updates (preserves battery)
    locManager.removeUpdates(this);
}
@Override
protected boolean isRouteDisplayed() {
    return false;
}

class MyGestureDetector extends SimpleOnGestureListener implements OnClickListener{
ArrayList<GeoPoint> points =new ArrayList<GeoPoint>(); 

@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
    // fetch the correspondent point from the map
    Log.d("A condition", "d5al l methodaaya");
    GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY());
    points.add(p);
    // create an overlay item and clear all others
    OverlayItem o = new OverlayItem(p, null, null);
  //  itemizedOverlay.clear();
    itemizedOverlay.addOverlay(o);

    // add the overlay item
    //mapView.getOverlays().clear();
    mapView.getOverlays().add(itemizedOverlay);
    mapView.invalidate();
    Geocoder geoCoder = new Geocoder(getBaseContext(),
            Locale.getDefault());
    // get the address based on the coordinates
    try {
        List<Address> addresses = geoCoder.getFromLocation(p.getLatitudeE6() / 1E6, p.getLongitudeE6() / 1E6, 1);
        String addressString = "";
        if (addresses.size() > 0) {
            for (int i = 0; i < addresses.get(0)
                    .getMaxAddressLineIndex(); i++)
                addressString += addresses.get(0).getAddressLine(i)
                        + " - ";
        }
        Toast.makeText(getBaseContext(), addressString,
                Toast.LENGTH_SHORT).show();

      //Add action item
        ActionItem addAction = new ActionItem();     
        addAction.setTitle("Add");
        addAction.setIcon(getResources().getDrawable(R.drawable.marker));
        //Accept action item
        ActionItem accAction = new ActionItem(); 
        accAction.setTitle("Accept");
        accAction.setIcon(getResources().getDrawable(R.drawable.marker));
        //Upload action item
        ActionItem upAction = new ActionItem();        
        upAction.setTitle("Upload");
        upAction.setIcon(getResources().getDrawable(R.drawable.marker ));
        final QuickAction mQuickAction  = new QuickAction(getBaseContext());
        mQuickAction.addActionItem(addAction);
        mQuickAction.addActionItem(accAction);
        mQuickAction.addActionItem(upAction);

现在的问题是,如果我尝试下面的带有路由的OnClickListener(这是一个按钮),它可以很好地工作,但如果我尝试整个地图,它绝对不会起任何作用。(如果有另一种完全不同的方式为引脚实现setOnClickListener,那就太棒了!)

//            mapView.setOnClickListener(new View.OnClickListener() {
//              @Override
//              public void onClick(View v) {
//                  mQuickAction.show(v);
//              }
//              });

        route.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mQuickAction.show(v);
            }
            });
        //setup the action item click listener
        mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
                public void onItemClick(int pos) {
                if (pos == 0) { //Add item selected
                   Toast.makeText(MyActivity.this, "Add item selected", Toast.LENGTH_SHORT).show();
                } else if (pos == 1) { //Accept item selected
                   Toast.makeText(MyActivity.this, "Accept item selected", Toast.LENGTH_SHORT).show();
                } else if (pos == 2) { //Upload item selected
                   Toast.makeText(MyActivity.this, "Upload items selected", Toast.LENGTH_SHORT).show();
                }
            }
                @Override
                public void onItemClick(QuickAction source, int pos,
                        int actionId) {
                     if (pos == 0) { //Add item selected
                           Toast.makeText(MyActivity.this, "Add item selected",      Toast.LENGTH_SHORT).show();
                        } else if (pos == 1) { //Accept item selected
                           Toast.makeText(MyActivity.this, "Accept item selected", Toast.LENGTH_SHORT).show();
                        } else if (pos == 2) { //Upload item selected
                           Toast.makeText(MyActivity.this, "Upload items selected", Toast.LENGTH_SHORT).show();
                        }
                }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
    return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    return super.onFling(e1, e2, velocityX, velocityY);
}
@Override
public boolean onDown(MotionEvent e) {
    return false;
}
@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
}
}

}

此外,我尝试了这里的建议,但没有奏效。Android为谷歌地图上的标记设置侦听器如有任何帮助,我们将不胜感激,提前感谢!

如果你想在用户点击标记时打开弹出菜单,那么首先删除以下代码:

itemizedOverlay.setGestureDetector(new GestureDetector(new MyGestureDetector()));

我想你不知道ItemizedOverlay中的onTap()方法,所以使用它而不是使用手势检测器,因为我认为你不能为不是视图的Android类使用手势检测器(我提供了下面的代码),你必须在你的MyItemizedOverlay类中使用以下代码:

@Override
public boolean onTap(int index)
{
 //Put the code for the creation of a pop-up menu, I'd suggest using a custom Dialog
 //box and putting options in it. However, you seem to be using ActionItems, I'm
 //not sure what they are.
}

最新更新