Cluster Marker not hiding when zoom in

泄露秘密 提交于 2019-12-05 13:40:09

Firstly, you need to define, when your ClusterManager should clusterize, so there is a method in MyClusterRenderer you should override:

@Override
protected boolean shouldRenderAsCluster(Cluster<MyItem> cluster) {
    return cluster.getSize() > 3; // when count of markers is more than 3, render as cluster
}

Secondly, when you make some changes with your markers such as adding, removing, changing title, icon, location, you should call cluster() method. So your addItems method should look like this:

private void addItems(List<LatLng> markers) {

    for (int i = 0; i < markers.size(); i++) {
        MyItem offsetItem = new MyItem(markers.get(i));
        mClusterManager.addItem(offsetItem);
    }
    mClusterManager.cluster();
}

EDIT:

I just look at your code one more time, and found that you forgot to set the clusterizing algorithm, it should be:

    ...
    mClusterManager = new ClusterManager<MyItem>(MapaViagem.this, googleMap);
    mClusterManager.setAlgorithm(new GridBasedAlgorithm<MyItem>());

    mClusterManager.setRenderer(new MyClusterRenderer(MapaViagem.this, googleMap, mClusterManager));
    ...

I had this issue and solved it by making sure that the ClusterManager was only being created and assigned once to the map, try putting and if statement in your create method. Most likely when you are initialising the map again in onResume that is causing the issue.

    public void onMapReady(GoogleMap googleMap) {
    map = googleMap;

    //only create one instance of cluster manager, 
    // if created again in onResume then click listeners don't work on reload and 
    //some markers don't disappear correctly on Zoom
    if (clusterManager == null){
    clusterManager =new ClusterManager(this, map);
     }

    map.setOnMarkerClickListener(clusterManager);
    map.setOnInfoWindowClickListener(clusterManager.getMarkerManager());
    map.setBuildingsEnabled(true);
    //noinspection MissingPermission
    map.setMyLocationEnabled(true);
    map.setOnCameraIdleListener(clusterManager);


    clusterManager.setOnClusterItemInfoWindowClickListener(this);


    clusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
                @Override
                public boolean onClusterItemClick(MyItem item) {
                    return false;
                }
            });
phtoxa

I know that this is an old post, but today I had the same issue.

After spend hour trying to solve, I finally got what happen with my application: The thing is that the cluster manager is working correctly, the problem is that my method which populates the map was being called twice.

To solve this issue just add mMap.clear(); before add markers. Works here like a charm!

Bye.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!