I want to change defult cluster group icon with my drawable icon. Please check my code for cluster program.
MyClusterRenderer.java
public class MyClusterRenderer extends DefaultClusterRenderer<MyItem> {
public MyClusterRenderer(Context context, GoogleMap map,
ClusterManager<MyItem> clusterManager) {
super(context, map, clusterManager);
}
@Override
protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {
super.onBeforeClusterItemRendered(item, markerOptions);
markerOptions.title("").icon(BitmapDescriptorFactory.fromResource(R.mipmap.location_both));// for marker
}
@Override
protected void onClusterItemRendered(final MyItem clusterItem, Marker marker) {
super.onClusterItemRendered(clusterItem, marker);
}
}
code in my fragment for setup cluser
googleMap.clear();
mClusterManager = new ClusterManager<MyItem>(getActivity(), googleMap);
googleMap.setOnMarkerClickListener(mClusterManager);
googleMap.setOnCameraChangeListener(mClusterManager);
readItems();
mClusterManager.setRenderer(new MyClusterRenderer(getActivity(), googleMap, mClusterManager));
Try to change Cluster icon in onBeforeClusterRendered
method instead of onBeforeClusterItemRendered
:
@Override
protected void onBeforeClusterRendered(Cluster<MyItem> cluster, MarkerOptions markerOptions) {
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.location_both));
}
The best way is
private val clusterIconGenerator = IconGenerator(context)
override fun getDescriptorForCluster(cluster: Cluster<PromotionMarker>): BitmapDescriptor {
clusterIconGenerator.setBackground(ContextCompat.getDrawable(context, R.drawable.background_marker))
val icon: Bitmap = clusterIconGenerator.makeIcon(cluster.size.toString())
return BitmapDescriptorFactory.fromBitmap(icon)
}
public class MyClusterRenderer extends DefaultClusterRenderer<MyItem> {
private final IconGenerator mClusterIconGenerator = new IconGenerator(
getActivity());
public MyClusterRenderer(Context context, GoogleMap map,
ClusterManager<MyItem> clusterManager) {
super(context, map, clusterManager);
View multiProfile = getActivity().getLayoutInflater().inflate(
R.layout.cluster_custome_icon, null);
mClusterIconGenerator.setContentView(multiProfile);
}
@Override
protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {
markerOptions.title("").icon(BitmapDescriptorFactory.fromResource(R.mipmap.location_both));
super.onBeforeClusterItemRendered(item, markerOptions);
}
// @Override
// protected void onClusterItemRendered(final MyItem clusterItem, Marker marker) {
// super.onClusterItemRendered(clusterItem, marker);
//
// }
@Override
protected void onBeforeClusterRendered(Cluster<MyItem> cluster,
MarkerOptions markerOptions) {
Log.e("get_item_list_nir", "CallMap onBeforeClusterRendered 13");
try {
mClusterIconGenerator.setBackground(null);
Bitmap icon = mClusterIconGenerator.makeIcon(String.valueOf(cluster
.getSize()));
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
} catch (Exception e) {
e.printStackTrace();
Log.e("get_item_list_nir", "error 13.1 : " + e.toString());
}
Log.e("get_item_list_nir", "CallMap onBeforeClusterRendered 14");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With