I've found that there is a way to change ClusterItem render options, customizing them this way:
clusterManager.setRenderer(new OwnIconRendered(getApplicationContext(), getMap(), clusterManager));
OwnIconRendered should extend DefaultClusterRenderer.
But, it changes all of the markers in one style. I need different styles options for different ClusterItems. I've tried to add markers in clusterManager collection manually, but then clustering doesn't work.
MarkerManager.Collection markerCollection = mClusterManager.getMarkerCollection();
for(MyMarker marker: markerList){
markerCollection.addMarker(getMarkerOptions(latLng, marker.getLabel(), marker.getType()));
}
I've figured it out. Clustering with different icons works great. First, I had to add some fields to my ClusterMarker class which implements ClusterItem.
public class ClusterMarker implements ClusterItem {
private final LatLng latLng;
private MyMarker myMarker;
...
public void setMyMarker(MyMarker myMarker) {
this.myMarker = myMarker;
}
}
MyMarker class has some info for styling markers, tilte and id's.
Later in the code I simply add markers to the ClusterManager instance this way:
mClusterManager.addItem(clusterMarker);
In OwnIconRendered class I overrided onBeforeClusterItemRendered method where all marker styling happening and in onClusterItemRendered method I added tags to markers, so later I could identify markers on map on clicking event.
public class OwnIconRendered extends DefaultClusterRenderer<ClusterMarker> {
...
cunstructor
...
@Override
protected void onBeforeClusterItemRendered(ClusterMarker item, MarkerOptions markerOptions) {
MyMarker myMarker = item.getMyMarker();
markerOptions.icon(myMarker.getIcon());
markerOptions.title(myMarker.getLabel());
markerOptions.anchor(0.5f, 1);
super.onBeforeClusterItemRendered(item, markerOptions);
}
@Override
protected void onClusterItemRendered(ClusterMarker clusterMarker, Marker marker) {
marker.setTag(clusterMarker.getPlaceMarker().getItemId());
super.onClusterItemRendered(clusterMarker, marker);
clusterMarkerMap.put(marker, clusterMarker);
}
}