简体   繁体   English

Google Maps API V3通过不同的图标指示同一位置的多个标记

[英]Google Maps API V3 Indicate multiple markers in same location by different icon

I'm working through all the code examples and have a page working that displays multiple markers. 我正在研究所有代码示例,并且有一个页面工作显示多个标记。 I have an array of locations and sometimes I have multiple markers with the same location. 我有一系列位置,有时我在同一位置有多个标记。 Only the top one displays, which is fine, but I want to indicate that there more markers hidden by changing the icon This bit works: 仅显示最上面的一个,这很好,但是我想通过更改图标来指示隐藏了更多标记。

for (i = 0; i < locations.length; i++) 
{  
  marker = new gm.Marker({
    position: new gm.LatLng(locations[i][1], locations[i][2]),
    map: map,
    icon: MarkerImg,
  });
}

I don't seem to be able to add conditional code to the "icon: MarkerImg" line to change it to a different icon if the coords match to the previous one. 我似乎无法将条件代码添加到“ icon:MarkerImg”行中,如果坐标与前一个匹配,则将其更改为其他图标。

Is there some way to have conditional declarations (if thats the correct terminology) Thanks. 有什么方法可以有条件声明(如果多数民众赞成在正确的术语)谢谢。

If all you need to do is check the previous marker, just keep a reference to it: 如果您需要做的只是检查上一个标记,只需保留对其的引用即可:

var lastMarker = null;
for (i = 0; i < locations.length; i++) 
{  
  var marker = new gm.Marker({
    position: new gm.LatLng(locations[i][1], locations[i][2]),
    map: map,
    icon: MarkerImg,
  });
  if (!!lastMarker && !!lastMarker.getPosition &&
      (marker.getPosition().equals(lastMarker.getPosition()))) {
       marker.setIcon(MarkerImg2);
  }
  lastMarker = marker;
}

fiddle 小提琴

Note: be careful with google.maps.LatLng.equals , the coordinates have to be exactly the same. 注意:请注意 google.maps.LatLng.equals ,坐标必须完全相同 Might want to pick a threshold (say 0.1 meter) and consider markers that are closer than that "at the same location". 可能希望选择一个阈值(例如0.1米),并考虑比“在同一位置”的标记更近的标记。

That did the trick, with a few edits: Yes, the markers will be coming in order, so that makes it easier. 做到了这一点,并进行了一些编辑:是的,标记将按顺序排列,因此更加容易。 I had to define a position for lastMarker before the equals would work. 在相等之前,我必须为lastMarker定义位置。 I picked 0,0 as I reckon its unlikely to be used. 我选择了0,0,因为我认为它不太可能被使用。 Good solution? 好的解决方案? Also I set the lastMarker to the current marker for the next iteration. 另外,我还将lastMarker设置为当前标记,以进行下一次迭代。 Good point on markers not exactly in the same position. 标记上的好点并不完全在同一位置。 For now they are, but its worth investigating that further. 目前它们是,但是值得进一步研究。

lastMarker = new gm.Marker(
    {
    position: new gm.LatLng(0,0)
    });

for (i = 0; i < locations.length; i++) 
    {  
    marker = new gm.Marker(
        {
        position: new gm.LatLng(locations[i][1], locations[i][2]),
        map: map,
        icon: MarkerImg,
        });
    if (marker.getPosition().equals(lastMarker.getPosition())) {
        marker.setIcon(MultiMarkerImg);
        }
    lastMarker = marker;

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

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