【问题标题】:Android > Google Maps > Overlay: Tap different things to make different things happenAndroid > Google Maps > Overlay:点击不同的东西,让不同的事情发生
【发布时间】:2011-12-31 21:53:28
【问题描述】:

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

我自己编写了第一个 onTap 方法,并从 Google 地图教程中复制了第二个方法以开始使用。问题是,第一个总是触发,而第二个永远不会触发。如果我删除第一种方法,第二种方法会按预期工作(点击标志会出现相应的对话框)。这两个都是ItemizedOverlay类中的方法,mContext是构造函数生成的上下文,locations是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;
    }

【问题讨论】:

    标签: java android google-maps overlay itemizedoverlay


    【解决方案1】:

    问题在于,通过实现/覆盖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;
    }
    

    希望对您有所帮助。

    【讨论】:

    • 太棒了。所以这就是我得到的结果:ItemizedOverlay 是标志的小图片的叠加,而我定义的类覆盖了整个地图。因此,当用户点击时,Android 首先检查用户是否点击了 ItemizedOverlay 中的某个位置(顺便说一下,唯一的挑剔是它是 super.onTap,而不是 super(onTap)),如果是,它会跳到另一个 onTap方法(对话框),如果没有,它会生成一个新标志。
    • 我还有一个问题是:以前,如果我点击一个标志,对话框不会出现,而是会在它后面创建一个新标志。我以为这是因为弹出对话框的方法被覆盖了,但似乎它仍然在框架内。你能更清楚地解释一下发生了什么,使这种情况不再发生吗?我也不太明白为什么 super.onTap(p, mapView) 如果用户点击标志会返回 true。谢谢!
    • onTap(GeoPoint p, MapView mapView)Overlay 中声明的方法,将由MapView 调用以响应触摸事件。 ItemizedOverlay 对此方法的实现包含将给定GeoPoint 映射到现有OverlayItem 的逻辑。如果找到OverlayItem,它将调用您的onTap(int index) 实现并返回返回的任何内容。如果它无法找到某个项目,则返回 false。因此,通过从 onTap(int index) 返回 true,您将停止在 onTap(GeoPoint p, MapView mapView) 实现中添加新项目。希望这是有道理的。
    • 我仍然不明白为什么以前单击现有的“OverlayItem”时它没有显示对话框。根据您所说,如果我的解释正确,我不希望它显示对话框然后制作新标志吗?
    • 不,因为之前您没有调用超类,并且您的 onTap(int index) 方法从未被调用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 2023-03-23
    • 2021-10-20
    • 2015-05-02
    • 2011-12-27
    • 1970-01-01
    相关资源
    最近更新 更多