【问题标题】:MarkerClusterPlus Cluster Size FormatMarkerClusterPlus 簇大小格式
【发布时间】:2017-03-05 19:25:14
【问题描述】:

我正在使用 Google Maps API 和 MarkerClusterPlus v3 在地图上渲染出许多图钉,然后将它们聚集在一起。问题是集群中可能有一个非常大的数字,比如 10000,所以我想将更大的数字格式化为“10k”而不是“10000”,这样它更干净,适合集群图像。

有人做过吗?我做了一些搜索,但没有找到任何方法来做到这一点。

谢谢!

【问题讨论】:

    标签: javascript google-maps-api-3 markerclusterer


    【解决方案1】:

    我不知道如何做到这一点,但您可以尝试使用 markerCluster.setCalculator 函数(如 here)更改标记的值。

    只需在返回值之前添加一个您需要的条件(在setCalculator 内),例如:

            if (count >= 1000) {
                var short = (count / 1000).toFixed(1) + 'k';
                return {
                    text: short,
                    index: index
                }
            } else {
                return {
                    text: count,
                    index: index
                };
            }
    

    【讨论】:

    • 我会接受这个答案。我做的有点不同,但这个想法来自你的回答。我正在使用 marker-clusterer-plus npm 包,所以我不能在其中添加代码,但它允许我通过在选项对象中传递它来覆盖任何函数,所以我只是重写它,传递它,然后添加到你的条件句,它就像一个魅力。
    猜你喜欢
    • 2018-10-28
    • 2020-07-09
    • 2013-05-05
    • 2012-01-13
    • 2013-11-26
    • 2017-07-15
    • 2021-01-21
    • 2014-06-03
    • 2015-10-14
    相关资源
    最近更新 更多