缩小时聚类地图标记,放大时不聚类

2023-12-04

我正在使用Google Map Android 集群实用程序具有 Google 地图 v2 播放服务。

我没有得到我预期的行为。正如您在下面的两张图片中看到的那样,当放大时,我可以看到 20 个簇和左侧的单个标记,但是当我缩小直到它们彼此重叠时,我没有看到它们簇。 . 20簇仍然显示20而不是21?

enter image description here

enter image description here

这是预期的行为吗?有没有办法可以让集群显示21而不是20+


这是指定的默认行为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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

缩小时聚类地图标记,放大时不聚类 的相关文章

随机推荐