简体   繁体   中英

Android > Google Maps > Overlay: Tap different things to make different things happen

I am trying to make an app where when the user taps on a blank part of the map, a new flag appears, and then when they tap the flag, a dialog box appears.

I wrote the first onTap method on my own, and copied the second from the Google Maps tutorial to get myself started. The problem is, the first one always fires and the second one never does. If I delete the first method, the second one works as it's supposed to (tapping a flag makes its corresponding dialog appear). These are both methods in an ItemizedOverlay class, mContext is the context generated by the constructor, and locations is an ArrayList of OverlayItems.

My question is, how can I reconcile the two?

    public boolean onTap(GeoPoint p, MapView mapView){
        locations.add(new OverlayItem(p, "Point 3", "Point 3"));
        populate();
        return false;
    }

    @Override
    protected boolean onTap(int index) {
      OverlayItem item = locations.get(index);
      AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
      dialog.setTitle(item.getTitle());
      dialog.setMessage(item.getSnippet());
      dialog.show();
      return true;
    }

The problem is that by implementing/overriding onTap(GeoPoint p, MapView mapView) you are preventing ItemizedOverlay 's own implementation of that method from running which itself would normally call onTap(int index) .

You want something more like...

public boolean onTap(GeoPoint p, MapView mapView){
    if (super.onTap(p, mapView)) 
        return true; 

    locations.add(new OverlayItem(p, "Point 3", "Point 3"));
    populate();
    return false;
}

@Override
protected boolean onTap(int index) {
  OverlayItem item = locations.get(index);
  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
  dialog.setTitle(item.getTitle());
  dialog.setMessage(item.getSnippet());
  dialog.show();
  return true;
}

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM