Google Maps API V3通过不同的图标指示同一位置的多个标记 [英] Google Maps API V3 Indicate multiple markers in same location by different icon

查看:78
本文介绍了Google Maps API V3通过不同的图标指示同一位置的多个标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究所有的代码示例,并有一个可以显示多个标记的页面。我有一系列位置,有时候我有多个位置相同的标记。只有最上面的一个显示,这很好,但我想指出有更多的标记隐藏在更改图标
这个位的作用:

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

我似乎无法将条件代码添加到图标:MarkerImg行来将它更改为不同的图标,如果坐标匹配上一个图标。

是否有某种方法可以进行条件声明(如果这是正确的术语)
Thanks。

解决方案

如果您只需检查上一个标记,只需保留一个引用即可:

  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,
}); (!lastMarker&& !!; lastMarker.getPosition&&
(marker.getPosition()。equals(lastMarker.getPosition()))){
marker。的setIcon(MarkerImg2);
}
lastMarker = marker;
}

小提琴



请注意 google.maps.LatLng.equals ,坐标必须完全一样。 完全相同。可能要选择一个阈值(比如0.1米),并考虑比在同一地点更接近的标记。


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.

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. Might want to pick a threshold (say 0.1 meter) and consider markers that are closer than that "at the same location".

这篇关于Google Maps API V3通过不同的图标指示同一位置的多个标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆