这是指定的默认行为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+"
to "21"
- 我会选择第一种方法
EDIT:
解决评论中提出的问题:
如果您想增加/减少分组项目的距离阈值 - 您可以修改默认算法用于聚类。只需使用这个常数(在你的情况下应该更小):
public static final int MAX_DISTANCE_AT_ZOOM = 100; // essentially 100 dp.
但正确的解决方法是考虑标记位图大小而不是常数值。我假设布罗德福德先生将其作为爱好者的作业:)
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);
}