Android 中的 Google 地图未调用 onClusterItemInfoWindowClick

Ish*_*han 2 android google-maps google-maps-markers

编辑:这个问题是关于 Android 上的谷歌地图。

我正在尝试为带有聚类的标记制作自定义信息窗口。我按照这里给出的说明/sf/answers/1537528541/

我做了这些:

设置集群管理器作为信息窗口适配器

// Setting custom cluster marker manager for info window adapter
    map.setInfoWindowAdapter(mClusterManager.getMarkerManager());
    mClusterManager.getMarkerCollection().setOnInfoWindowAdapter(new MyLocationInfoWindowAdapter());
Run Code Online (Sandbox Code Playgroud)

为 clusterItem 信息窗口设置单击侦听器

mClusterManager.setOnClusterItemInfoWindowClickListener(new ClusterManager.OnClusterItemInfoWindowClickListener<LocationMarker>() {
        @Override
        public void onClusterItemInfoWindowClick(LocationMarker locationMarker) {
            Toast.makeText(context, "info window Click", Toast.LENGTH_SHORT).show();
        }
    });
Run Code Online (Sandbox Code Playgroud)

这是我的信息窗口适配器

private class MyLocationInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }

    @Override
    public View getInfoContents(Marker marker) {
        TextView helloTextView = new TextView(getContext());
        helloTextView.setText("Hello Info contents");
        helloTextView.setClickable(false);
        return helloTextView;
    }
}
Run Code Online (Sandbox Code Playgroud)

Ish*_*han 5

我找到了解决方案。我把它张贴在这里,以便其他人可以发现它有用。

似乎即使在将 InfoWindowAdapter 设置为我的自定义 ClusterManager 的 MarkerManager 之后,如下所示:

map.setInfoWindowAdapter(mClusterManager.getMarkerManager());

信息窗口的点击侦听器仍然是map对象而不是mClusterManager.

所以为了响应信息窗口的点击,我只需要map像这样设置它:

map.setOnInfoWindowClickListener(new MyMarkerInfoWindowClickListener());

这里要带回家的一点是,信息窗口的点击不会在集群管理器中注册,而只保留在地图上。

  • 您还可以通过显式调用 ClusterManager 上的 onInfoWindowClick(marker) 函数(可能在地图对象的 OnInfoWindowClickListener 中)调用 ClusterManager 上的 OnClusterItemInfoWindowClickListener 或 OnClusterInfoWindowClickListener。 (2认同)