我正在尝试制作一个应用程序,其中当用户点击地图的空白部分时,会出现一个新的标志,然后当他们点击该标志时,会出现一个对话框。

我自己编写了第一个onTap方法,然后从Google Maps教程中复制了第二个方法,以使自己入门。问题是,第一个始终触发,第二个从未触发。如果删除第一种方法,则第二种方法应按预期的方式工作(点按一个标志会使相应的对话框出现)。这两个都是ItemizedOverlay类中的方法,mContext是构造函数生成的上下文,而location是OverlayItems的ArrayList。

我的问题是,我该如何调和两者?

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

最佳答案

问题在于,通过实现/覆盖onTap(GeoPoint p, MapView mapView),可以防止ItemizedOverlay自己对该方法的实现运行,而该方法本身通常会调用onTap(int index)

您想要更多类似...

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


希望能有所帮助。

10-05 20:56
查看更多