简体   繁体   English

Markerclusterer 根据其中的标记设置标记簇图标

[英]Markerclusterer set marker cluster icon depending on markers inside it

Let's say that I want to make a webpage that will show approximate number of free parking spots left on each parking(due to confidentiality I'm not allowed to give any details of what I'm actually doing).假设我想制作一个网页,显示每个停车位上剩余的免费停车位的大致数量(由于保密,我不允许提供我实际在做什么的任何细节)。 For this I'm using Google maps and Markerclusterer .为此,我使用Google 地图Markerclusterer So for parking with less than 5% spots left I'd like to show a red marker, for parkings with 5%-25% spots I'd show a yellow one and for ones with more than 25% free spots I want to make it green.因此,对于剩余停车位少于 5% 的停车位,我想显示一个红色标记,对于停车位为 5%-25% 的停车位,我会显示一个黄色的停车位,对于空闲停车位超过 25% 的停车位我想显示它是绿色的。 So far I could make those markers and cluster them, but here's the tricky part(and question itself):到目前为止,我可以制作这些标记并将它们聚类,但这是棘手的部分(以及问题本身):

How can I make a cluster icon dependant on the markers inside it?如何使集群图标依赖于其中的标记?

For example:例如:

  • Parking A is green停车场A是绿色的
  • Parking B is red停车场B是红色的
  • Parking C is green停车场C是绿色的
  • Parking D is yellow停车D是黄色的

When zoomed out I want to show the cluster that has all 4 of them red(worst of all).当缩小时,我想显示所有 4 个红色的集群(最糟糕的是)。 When zoomed in I'd get 2 clusters(A+B and C+D).放大时,我会得到 2 个集群(A+B 和 C+D)。 I want the first cluster(A+B) to be red and second(C+D) should be yellow.我希望第一个集群(A+B)是红色的,第二个(C+D)应该是黄色的。

What I did so far:到目前为止我做了什么:

var mcOptions = {gridSize: 50, maxZoom: 15, styles: [{
    height: 46,
    url: "///Users/Novarg/Downloads/foundation-5.4.7/img/greenC.png",
    width: 46
  },
  {
    height: 46,
    url: "///Users/Novarg/Downloads/foundation-5.4.7/img/redC.png",
    width: 46
  }]};
  var markers = [];
  for (var i = 0; i < 100; i++) {
    var latLng = new google.maps.LatLng(51 + Math.random(),
      4 + Math.random());
    var marker = new google.maps.Marker({'position': latLng, icon: 'img/greenP.png'});
    markers.push(marker);
  }
  for (var i = 0; i < 20; i++) {
    var latLng = new google.maps.LatLng(51 - Math.random(),
      4 - Math.random());
    var marker = new google.maps.Marker({'position': latLng, icon: 'img/redP.png'});
    markers.push(marker);
  }
  var markerCluster = new MarkerClusterer(map, markers, mcOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);

Right now I only have red and green markers, which should be enough to test it.现在我只有红色和绿色标记,应该足以测试它。 But behaviour of this cluster now is as follows:但是这个集群现在的行为如下:

  • All clusters with less than 10 markers in it are green所有少于 10 个标记的集群都是绿色的
  • All clusters with more than 9 markers in it are red所有包含超过 9 个标记的集群都是红色的

EDIT编辑

From this link I found that what I might need is Calculator .这个链接我发现我可能需要的是Calculator So I tried it, but still no luck(although I think that I'm getting closer. Actually, I hope I'm very close to the solution right now).所以我尝试了它,但仍然没有运气(虽然我认为我越来越接近了。实际上,我希望我现在非常接近解决方案)。

So I tried to change my options:所以我试图改变我的选择:

var mcOptions = {gridSize: 50, maxZoom: 15, styles: [{
  height: 46,
  url: "///Users/Novarg/Downloads/foundation-5.4.7/img/greenC.png",
  width: 46
},
{
  height: 46,
  url: "///Users/Novarg/Downloads/foundation-5.4.7/img/redC.png",
  width: 46
}],
calculator: function(markers, numStyles) {
  for (var i = 0; i < markers.length; i++) {
    if (markers[i].getIcon().indexOf("redP.png") > -1) {
      return {text: markers.length, index: 1};
    }
  }
  return {text: markers.length, index: 0};
}
};

But the Calculator is never being used.但是从未使用过计算器 I tested it by putting a simple alert('test');我通过放置一个简单的alert('test'); inside it.在里面。

I hope this additional info will help you to help me find a solution.我希望这些附加信息可以帮助您帮助我找到解决方案。

As I already mentioned in the edit, I was very close to the solution.正如我在编辑中已经提到的,我非常接近解决方案。 So I took another(fresh) look at the code today, checked the docs once again and noticed the following in the ClusterIconInfo :因此,我今天又(重新)查看了代码,再次检查了文档,并在ClusterIconInfo 中注意到了以下内容:

index number The index plus 1 of the element in the styles array to be used to style the cluster icon.索引号 样式数组中元素的索引加 1 ,用于设置集群图标的样式。

So basically I solved this problem simply by incrementing the index by one(and I also moved Calculator to be a var and then used setCalculator() method on the MarkerClusterer itself).所以基本上我只是通过将索引增加 1 来解决这个问题(我还将Calculator移动为一个 var,然后在MarkerClusterer本身上使用setCalculator()方法)。 So my code became:所以我的代码变成了:

var calc = function(markers, numStyles) {
  for (var i = 0; i < markers.length; i++) {
    if (markers[i].getIcon().indexOf("redP") > -1) {
      return {text: markers.length, index: 2};
    }
  }
  return {text: markers.length, index: 1};
}

var mcOptions = {gridSize: 50, maxZoom: 15, styles: [{
    height: 46,
    url: "img/greenC.png",
    width: 46
  },
  {
    height: 46,
    url: "img/redC.png",
    width: 46
  }]
};

var markerCluster = new MarkerClusterer(map, markers, mcOptions);
markerCluster.setCalculator(calc);

And now it works like a charm(as it should).现在它就像一个魅力(应该)。

Hopefully this could help somebody someday.希望有一天这可以帮助某人。

Extending on Novarg's answer, you will need to define a Calculator , which selects a style from the initial styles array passed into the MarkerClusterer 's constructor.扩展 Novarg 的答案,您将需要定义一个Calculator ,它从传递给MarkerClusterer的构造函数的初始样式数组中选择一个样式。

If you want to extend the original functionality (which controls the style based on the number of markers the Cluster contains, you can add styles in multiples of 3 in the initial styles array and call the original Calculator in your calculator.如果您想扩展原始功能(根据Cluster包含的标记数量控制样式,您可以在初始样式数组中添加 3 的倍数样式,并在Calculator中调用原始Calculator

// create MarkerClusterer instance with array of styles, multiples of 3
const markerClusterer = new MarkerClusterer(this.map, this.kioskMarkers, {
  clusterClass: "custom-clustericon",
  styles: [
    {
      width: 30,
      height: 30,
      className: "normal"
    },
    {
      width: 40,
      height: 40,
      className: "normal"
    },
    {
      width: 50,
      height: 50,
      className: "normal"
    },
    {
      width: 30,
      height: 30,
      className: "special"
    },
    {
      width: 40,
      height: 40,
      className: "special"
    },
    {
      width: 50,
      height: 50,
      className: "special"
    }]
});

// your function that determines special styling
const checkForYourSpecialCondition = () => true;

// new calculator function
const newCalculator = (markers, numStyles) => {
    const offset = checkForYourSpecialCondition() ? 3 : 0;
    const { text, index } = MarkerCluster.CALCULATOR(markers, numStyles);
    return {text, index: index + offset};
}

markerClusterer.setCalculator(newCalculator);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM