0

Here is my main activity class:

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

Now here's the problem, if I try the below OnClickListener with route (which is a button) it works fine, but if I try it for the whole map it does absolutely nothing. (if there's another totally different way which implements a setOnClickListener for the pins that'd be great!)

//            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

}
}


}

Also, I tried what's suggested here but it didn't work. Android set Listener for marker on google map Any help would be greatly appreciated, thanks in advance!

4

1 回答 1

0

如果您希望在用户单击标记时打开弹出菜单,则首先删除以下代码:

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

我认为您不知道 ItemizedOverlay 中的 onTap() 方法,因此请使用它而不是使用 GestureDetector 因为我认为您不能将 GestureDetector 用于不是视图的 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.

}
于 2012-08-29T21:44:12.283 回答