I created Custom Cluster Render
extending DefaultClusterRenderer<T>
, while using Google Maps Api
and android maps utils
. According to the following guide
https://github.com/googlemaps/android-maps-utils/blob/master/demo/src/com/google/maps/android/utils/demo/CustomMarkerClusteringDemoActivity.java
When I have more then too many markers
in the same area, they cluster into the one. But the don't separate onto markers
while zooming, and I can see
http://cs627121.vk.me/v627121202/51eb5/rY9f4p8ivos.jpg
Here's some code:
public PlaceRender(Context context, GoogleMap map, ClusterManager<MyCluster> clusterManager) {
super(context, map, clusterManager);
mIconGenerator = new IconGenerator(context);
imageView = new ImageView(context);
mClusterImageView = new ImageView(context);
mIconGenerator.setContentView(imageView);
this.context = context;
this.map = map;
this.myClusterManager = clusterManager;
}
@Override
protected void onBeforeClusterItemRendered(MyCluster cluster, MarkerOptions markerOptions) {
super.onBeforeClusterItemRendered(cluster, markerOptions);
markerOptions.title(cluster.name).snippet(cluster.snippet);
markerOptions.infoWindowAnchor(0.5f, 0.0f);
}
@Override
protected void onBeforeClusterRendered(Cluster<MyCluster> cluster, MarkerOptions markerOptions) {
super.onBeforeClusterRendered(cluster, markerOptions);
}
@Override
protected boolean shouldRenderAsCluster(Cluster cluster) {
// Always render clusters.
return cluster.getSize() > 1;
}
@Override
protected void onClusterRendered(Cluster<MyCluster> cluster, Marker marker){
super.onClusterRendered(cluster, marker);
}
@Override
protected void onClusterItemRendered(MyCluster geo, Marker marker) {
super.onClusterItemRendered(geo, marker);
loadIconFromApi(geo, marker);
}
public void addItems(List<MyCluster> clusters, Context context, GoogleMap map) {
myClusterManager = new ClusterManager<>(context, map);
map.setOnMarkerClickListener(this);
for (int i = 0; i < clusters.size(); i++) {
myClusterManager.addItem(clusters.get(i));
myClusterManager.setRenderer(new PlaceRender(context, map, myClusterManager));
}
myClusterManager.cluster();
}
//
private void loadIconFromApi(MyCluster geo, Marker marker) {
PicassoMarker markers = new PicassoMarker(marker, mIconGenerator, imageView);
geo.setPicassoMarker(markers);
Picasso.with(context).load(geo.image).into(markers);
}
@Override
public boolean onMarkerClick(Marker marker) {
marker.showInfoWindow();
return true;
}
If I use 15x camera zoom
, so I can see every marker, and then zoom out
it, my markers won't merge into the one cluster
.
What did I missed?
I needed to add this function and call it in onCameraChange
public void updateUI(List<MyCluster> clusters) {
myClusterManager.clearItems();
for (int i = 0; i < clusters.size(); i++) {
myClusterManager.addItem(clusters.get(i));
// myClusterManager.setRenderer(new PlaceRender(context, map, myClusterManager));
}
myClusterManager.onCameraChange(map.getCameraPosition());
}