如何在Google Map ClusterManager上实现事件监听器?

时间:2019-01-14 20:12:14

标签: android google-maps google-maps-markers

我正在使用utils库中提供的GoogleMaps标记群集。单击Cluster时,不会调用下面的onClusterClick()onClusterItemClick()方法。有集群点击事件吗?

@Override
public void onMapReady(GoogleMap googleMap) {

    /*start clustring*/

    clusterManager = new ClusterManager<>(getActivity(), map);
    map.setOnCameraIdleListener(clusterManager);

    /*end clustring*/



    LatLng karawanbazar1 = new LatLng(36.861666, 10.156551);
    LatLng karawanbazar2 = new LatLng(36.860179, 10.152874);



    MyItemMap myItemMap1 = new MyItemMap(karawanbazar1);
    MyItemMap myItemMap2 = new MyItemMap(karawanbazar2);


    clusterManager.addItem(myItemMap1);

    clusterManager.addItem(myItemMap2);


    clusterManager.cluster();
    clusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItemMap>() {
        @Override
        public boolean onClusterItemClick(MyItemMap myItemMap) {
            Toast.makeText(getContext(),"onclusterItemClick"+ myItemMap.getPosition(),Toast.LENGTH_LONG);
            System.out.println("this is cluster clusteItemClick "+myItemMap.toString() );
            return true;
        }
    });
    clusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<MyItemMap>() {
        @Override
        public boolean onClusterClick(Cluster<MyItemMap> cluster) {
            Toast.makeText(getContext(),"setOnClusterClickListener"+ cluster.getSize(),Toast.LENGTH_LONG);
            System.out.println("this is cluster onClusterClick "+cluster.toString() );

            return true;
        }
    });
}

2 个答案:

答案 0 :(得分:1)

可能您正在寻找setOnMarkerClickListener()对象的方法GoogleMap

googleMap.setOnMarkerClickListener(clusterManager)

然后您必须设置:

clusterManager.setOnClusterClickListener()

答案 1 :(得分:0)

我要做的就是在群集上使用事件之前添加此行。

  map.setOnMarkerClickListener(yourClusterManager);