我正在尝试制作一个应用程序,当用户点击 map 的空白部分时,会出现一个新标志,然后当他们点击该标志时,会出现一个对话框。
我自己编写了第一个 onTap 方法,并从 Google map 教程中复制了第二个方法来开始使用。问题是,第一个总是会触发,而第二个永远不会。如果我删除第一个方法,第二个方法将按预期工作(点击标志会出现相应的对话框)。这些都是 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;
}
最佳答案
问题是,通过实现/覆盖 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;
}
希望有帮助。
关于java - Android > 谷歌地图 > 叠加层 : Tap different things to make different things happen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8690749/