enter code here我需要在Google Map上显示标记的集群。
我有自定义对象:

public class PostClusterItem implements ClusterItem {

private final LatLng mPosition;
BitmapDescriptor icon;
String title;
String desc;
}


我想将其传递给ClusterManager。
我从服务器收到一组对象,并在回调中将它们全部解析:

 @Override
public void onAllPostsLoaded(List<Post> postList) {
    mClusterManager = new ClusterManager<PostClusterItem>(getActivity(), googleMap);
    mClusterManager.clearItems();
    googleMap.setOnCameraChangeListener(mClusterManager);
    googleMap.setOnMarkerClickListener(mClusterManager);
    for (Post post : postList) {
        addMarkers(post);
    }
    mClusterManager.setRenderer(new CustomClusterRenderer(getActivity(), googleMap, mClusterManager));
}


我的addMarkers方法:

  private void addMarkers(Post post) {
    PostClusterItem postClusterItem = new PostClusterItem(post.getLat(), post.getLng(), post.getTitle(), post.getDescription());
    mClusterManager.addItem(postClusterItem);

}


但我不知道原因,clusterManager的工作情况很差:
java - Google API ClusterManager自定义项呈现-LMLPHP

标记未正确添加到集群中,我可以得到下一种情况:
java - Google API ClusterManager自定义项呈现-LMLPHP

但是当我对一些简单对象进行测试时,这种情况不存在:

public class MyItem implements ClusterItem {
    private final LatLng mPosition;

    public MyItem(double lat, double lng) {
        mPosition = new LatLng(lat, lng);
    }
}


这可能是什么原因?

这是我的CustomClusterRenderer

public class CustomClusterRenderer extends DefaultClusterRenderer<PostClusterItem> {

    public CustomClusterRenderer(Context context, GoogleMap map,
                       ClusterManager<PostClusterItem> clusterManager) {
        super(context, map, clusterManager);
    }


        protected void onBeforeClusterItemRendered(PostClusterItem item, MarkerOptions markerOptions) {
            super.onBeforeClusterItemRendered(item, markerOptions);
            markerOptions.snippet(item.getDesc());
            markerOptions.title(item.getTitle());

        }
    }

最佳答案

也许为时已晚,但总是打电话

mClusterManager.cluster();


添加项目后。

这是此处的ClusterManager的源代码:
https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/clustering/ClusterManager.java

这里是一些解释:

/**
 * Force a re-cluster. You may want to call this after adding new item(s).
 */
public void cluster() {
...

10-08 02:01