这是DefaultClasterRenderer#onBeforeClusterRendered() 中指定的默认行为:
/**
* Called before the marker for a Cluster is added to the map.
* The default implementation draws a circle with a rough count of the number of items.
*/
protected void onBeforeClusterRendered(Cluster<T> cluster, MarkerOptions markerOptions) {
int bucket = getBucket(cluster);
BitmapDescriptor descriptor = mIcons.get(bucket);
if (descriptor == null) {
mColoredCircleBackground.getPaint().setColor(getColor(bucket));
descriptor = BitmapDescriptorFactory.fromBitmap(mIconGenerator.makeIcon(getClusterText(bucket)));
mIcons.put(bucket, descriptor);
}
// TODO: consider adding anchor(.5, .5) (Individual markers will overlap more often)
markerOptions.icon(descriptor);
}
请注意,标记的文本是根据bucket 选择的,而不是cluster 中的确切项目数
对此的快速解决方法是将描述符创建修改为:
descriptor = BitmapDescriptorFactory.fromBitmap(mIconGenerator.
makeIcon(cluster.getSize());
当然,您可以实现您的自定义ClasterRenderer 并将其提供给ClusterManager。这样,您将负责标记的渲染,但如果您只想将 "20+" 更改为 "21" - 我会采用第一种方法
编辑:
解决 cmets 中提出的问题:
如果您想增加/减少分组项目的距离阈值 - 您可以修改用于聚类的default algorithm。只需使用这个常量(在你的情况下应该更小):
public static final int MAX_DISTANCE_AT_ZOOM = 100; // essentially 100 dp.
但正确的解决方法是考虑标记位图大小而不是常量值。我假设Mr. Broadfood 把它作为给爱好者的家庭作业:)
private Bounds createBoundsFromSpan(Point p, double span) {
// TODO: Use a span that takes into account the visual size of the marker, not just its
// LatLng.
double halfSpan = span / 2;
return new Bounds(
p.x - halfSpan, p.x + halfSpan,
p.y - halfSpan, p.y + halfSpan);
}