Google API ClusterManager自定义项呈现

时间:2016-01-28 08:23:10

标签: java android google-maps

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

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工作得很糟糕: enter image description here

标记没有正确添加到群集中,我可以得到和下一个情况: enter image description here

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

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());

        }
    }

1 个答案:

答案 0 :(得分:0)

可能为时已晚,但总是打电话

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() {
...