gpt4 book ai didi

java - Google API ClusterManager 自定义项目渲染

转载 作者:太空宇宙 更新时间:2023-11-04 12:59:19 26 4
gpt4 key购买 nike

在此处输入代码我需要在 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 工作得很糟糕: 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());

}
}

最佳答案

也许为时已晚,但一定要打电话

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

关于java - Google API ClusterManager 自定义项目渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35056208/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com