Java android谷歌地图集群,点击它们后更改标记中的图标



在我的应用程序中,我有很多点要在谷歌地图中显示,所以我决定使用ClusterManager。我想更改一个 ClusterItem 上的图标(我单击了它(,这是我怎么能做到这一点的任何想法:

我这样做:

@Override public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mClusterManager = new ClusterManager<>(this, mMap);
final CustomClusterRenderer renderer = new CustomClusterRenderer(this, mMap, mClusterManager);
mClusterManager.setRenderer(renderer);
mClusterManager.setOnClusterClickListener(
new ClusterManager.OnClusterClickListener<StringClusterItem>() {
@Override public boolean onClusterClick(Cluster<StringClusterItem> cluster) {
Toast.makeText(MapsActivity.this, "Cluster click", Toast.LENGTH_SHORT).show();
return false;
}
});
mClusterManager.setOnClusterItemClickListener(
new ClusterManager.OnClusterItemClickListener<StringClusterItem>() {
@Override public boolean onClusterItemClick(StringClusterItem clusterItem) {
Toast.makeText(MapsActivity.this, "Cluster item click", Toast.LENGTH_SHORT).show();
return false;
}
});
mClusterManager.setOnClusterItemInfoWindowClickListener(
new ClusterManager.OnClusterItemInfoWindowClickListener<StringClusterItem>() {
@Override public void onClusterItemInfoWindowClick(StringClusterItem stringClusterItem) {
Toast.makeText(MapsActivity.this, "Clicked info window: " + stringClusterItem.title,
Toast.LENGTH_SHORT).show();
}
});
mMap.setOnInfoWindowClickListener(mClusterManager);
mMap.setInfoWindowAdapter(mClusterManager.getMarkerManager());
mMap.setOnCameraChangeListener(mClusterManager);
mMap.setOnMarkerClickListener(mClusterManager);
for (int i = 0; i < 10; i++) {
final LatLng latLng = new LatLng(-34 + i, 151 + i);
mClusterManager.addItem(new StringClusterItem("Marker #" + (i + 1), latLng));
}
mClusterManager.addItem(new StringClusterItem("Marker #" + (10 + 1), new LatLng(-34+11 , 151 +11),true));
mClusterManager.cluster();
}

如果您查看他们提到的文档自定义标记集群

CustomMarkerClusteringDemoActivity 定义了自己的集群项,即 Person,并通过扩展 DefaultClusterRenderer 将其呈现为 人物渲染器。

您也可以以类似的方式实现ClusterManager.OnClusterItemClickListener<Person>

他们还提供了示例代码来更改 ClusterItem 的图标/图像。

相关内容

  • 没有找到相关文章