1

我编写了以下方法来显示我的标记(聚集)。但是,一些集群工作正常 - 即当我放大时,蓝色集群图标(前表示 10+)消失并显示各个标记。但是,有时,当我完全放大时,蓝色簇标记仍会与单个标记之一一起显示在完全缩放中。为什么会这样?

private void displayCluster(List<MyItem> items) {

      mClusterManager = new ClusterManager<MyItem>(this, googleMap);
      mClusterManager.setRenderer(new MyRender(getApplicationContext(),googleMap,mClusterManager));

      googleMap.setOnCameraChangeListener(mClusterManager);
      googleMap.setOnMarkerClickListener(mClusterManager);

       for (MyItem item : items) {
           mClusterManager.addItem(item);
       }
   }
4

1 回答 1

6

DefaultClusterRenderer 决定标记是否应该被聚类。这个类包含 DefaultClusterRenderer#shouldRenderAsCluster() 方法 - 只有当聚类大小> MIN_CLUSTER_SIZE 时才开始聚类。MIN_CLUSTER_SIZE 的默认值为 4。

您需要扩展 DefaultClusterRenderer 类并覆盖 shouldRenderAsCluster() 方法以提供您自己的逻辑:

class CustomRenderer<T extends ClusterItem> extends DefaultClusterRenderer<T>
{
    public CustomRenderer(Context context, GoogleMap map, ClusterManager<T> clusterManager) {
        super(context, map, clusterManager);            
    }

    @Override
    protected boolean shouldRenderAsCluster(Cluster<T> cluster) {
        //start clustering if at least 2 items overlap
        //Change your logic here
        return cluster.getSize() > 1;
    }
}

private void setUpMapIfNeeded() {
    if (mMap != null) {
        return;
    }
    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    if (mMap != null) {
        mClusterManager = new ClusterManager<Place>(this, mMap);
        mClusterManager.setRenderer(new CustomRenderer<Place>(this, mMap, mClusterManager));
        ........
    }
}
于 2015-11-30T06:29:08.170 回答