使用标记群集器自定义显示编号

时间:2019-01-26 03:25:13

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


作为附件,群集器在伦敦显示 417 标记。
我想按阈值自定义一点。例如

  • <= 20: Display real number
  • From 20 to 50: 20+
  • From 50 to 100: 50+
  • From 100 to 200: 100+
  • > 200: 200+

如何将阈值应用于群集器?

enter image description here

1 个答案:

答案 0 :(得分:2)

我找到了一种更改群集器中显示号的绝妙方法。
通过更改计算器方法

markerCluster.setCalculator(new_cluster_calculator);

function new_cluster_calculator (markers, numStyles) {
  var index = 0;
  var title = "";
  var count = markers.length;
  var countStr = count.toString();

  var dv = countStr;
  while (dv !== 0) {
    dv = parseInt(dv / 10, 10);
    index++;
  }

  index = Math.min(index, numStyles);

  if (count >= 200) {
    count = '200+';
  } else if (count >= 100) {
    count = '100+';
  } else if (count >= 50) {
    count = '50+';
  }

  return {
    text: count,
    index: index,
    title: title
  };
};