简体   繁体   English

Android> Google Maps>叠加层:点按不同的内容以使不同的事情发生

[英]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. 我自己编写了第一个onTap方法,然后从Google Maps教程中复制了第二个方法来开始使用。 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. 这两个都是ItemizedOverlay类中的方法,mContext是构造函数生成的上下文,而location是OverlayItems的ArrayList。

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) . 问题在于,通过实现/覆盖onTap(GeoPoint p, MapView mapView)您将无法运行ItemizedOverlay对该方法的实现,而该方法本身通常会调用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. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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