Reputation: 661
When I have a cluster 17, 18, it shows "10+", "20+". How can I display the exact number?
Thanks.
Upvotes: 1
Views: 2238
Reputation: 760
Google Map by default provide approximate number of clustered items. To achieve exact number of clustered item, just override these two methods in your custom cluster renderer class:
@Override
protected String getClusterText(int bucket) {
return String.valueOf(bucket);
}
@Override
protected int getBucket(Cluster<MyItem> cluster) {
return cluster.getSize();
}
The complete code snippet is :
public class MarkerClusterRenderer extends DefaultClusterRenderer<MyItem> {
MarkerClusterRenderer(Context context, GoogleMap map, ClusterManager<MyItem> clusterManager) {
super(context, map, clusterManager);
}
@Override
protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {
// use this to make your change to the marker option
// for the marker before it gets render on the map
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_map_pins));
}
@Override
protected void onClusterItemRendered(MyItem item, Marker marker) {
//set marker title, tag, snippet, e.t.c here
}
@Override
protected String getClusterText(int bucket) {
return String.valueOf(bucket);
}
@Override
protected int getBucket(Cluster<MyItem> cluster) {
return cluster.getSize();
}
}
Here is the result :
Happy Coding :)
Upvotes: 3
Reputation: 1
This one is a bit old, but since it helped me find an easier solution I thought I post here.
Berdimurat has the right idea except the solution is simpler.
@Override
protected int getBucket(Cluster<FlagItemCluster> cluster) {
return cluster.getSize();
}
@Override
protected String getClusterText(int bucket) {
return String.valueOf(bucket);
}
Basically rather than using onBeforeClusterRendered, just alter where it gets it's data. This method also corrects that the number displayed isn't accurate because of what the getBucket logic contains.
Upvotes: 0
Reputation: 345
public class MyClusterItemRenderer extends DefaultClusterRenderer<MyClusterItem> {
private final IconGenerator mIconGenerator;
private final ShapeDrawable mColoredCircleBackground;
private final float mDensity;
private SparseArray<BitmapDescriptor> mIcons = new SparseArray();
public MyClusterItemRenderer(Context context, GoogleMap map, ClusterManager<MyClusterItem> clusterManager) {
super(context, map, clusterManager);
mIconGenerator = new IconGenerator(context);
mColoredCircleBackground = new ShapeDrawable(new OvalShape());
mDensity = context.getResources().getDisplayMetrics().density;
SquareTextView squareTextView = new SquareTextView(context);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(-2, -2);
squareTextView.setLayoutParams(layoutParams);
squareTextView.setId(com.google.maps.android.R.id.text);
int twelveDpi = (int)(12.0F * this.mDensity);
squareTextView.setPadding(twelveDpi, twelveDpi, twelveDpi, twelveDpi);
this.mIconGenerator.setContentView(squareTextView);
mIconGenerator.setTextAppearance(com.google.maps.android.R.style.ClusterIcon_TextAppearance);
ShapeDrawable outline = new ShapeDrawable(new OvalShape());
outline.getPaint().setColor(Color.parseColor("#FFFFFF"));
LayerDrawable background = new LayerDrawable(new Drawable[]{outline, this.mColoredCircleBackground});
int strokeWidth = (int) (this.mDensity * 3.0F);
background.setLayerInset(1, strokeWidth, strokeWidth, strokeWidth, strokeWidth);
mIconGenerator.setBackground(background);
}
@Override
protected void onBeforeClusterRendered(Cluster<MyClusterItem> cluster, MarkerOptions markerOptions) {
int bucket = this.getBucket(cluster);
BitmapDescriptor descriptor = (BitmapDescriptor)this.mIcons.get(bucket);
if(descriptor == null) {
this.mColoredCircleBackground.getPaint().setColor(this.getColor(bucket));
descriptor = BitmapDescriptorFactory.fromBitmap(this.mIconGenerator.makeIcon(String.valueOf(cluster.getSize())));
this.mIcons.put(bucket, descriptor);
}
markerOptions.icon(descriptor);
}
}
on activity:
clusterManager.setRenderer(new MyClusterItemRenderer(getApplicationContext(), mGoogleMap, clusterManager));
Upvotes: 4
Reputation: 2334
You can implement ClusterRenderer to customize the rendering of the clusters. DefaultClusterRenderer provides a good base to start from. By subclassing DefaultClusterRenderer, you can override the defaults.
For an in-depth example of customization, take a look at CustomMarkerClusteringDemoActivity in the demo app that ships with the utility library.
Upvotes: 0