HTML/JS - Google Maps v3 -从外部链接中选择/删除标记

HTML/JS - Google Maps v3 - select/remove marker by id from external link

本文关键字:选择 删除 链接 v3 JS Google Maps HTML 从外部      更新时间:2023-09-26

我是谷歌地图和javascript的新手。我正在尝试使用外部链接/列表与地图进行交互。

我的主要问题是我无法选择地图上的标记与它的id。

我不想把所有的标记放在列表/数组中并遍历它们,因为我不知道在任何时候会有多少个标记。

我只是想通过他们的id选择他们,并与他们一起工作,如打开infowindow,删除他们等…

在我的changeIcon()函数,我尝试了一些方法,但没有工作到目前为止。

<html>
....
<div id="mapview"></div>
<a class="test-link" onmouseover="changeIcon()">Link</a>
<script>
function changeIcon()
    {
        //marker = map.selectMarker("4");
        //marker = markers[4];
        //infowindow.open(map,marker);
        //$("#mapview").map.removeMarker(4);
    }
var map = new google.maps.Map(document.getElementById('mapview'), 
        {
            center: {lat: 44.540, lng: -78.546},
            zoom: 16
        });
function initMap() 
{
  var marker = new google.maps.Marker({
          position: userPosition,
          map: map,
          id: 4
        });
}
</script>
</html>

............................................

没有API从map中获取标记。裁判:"https://developers.google.com/maps/documentation/javascript/reference"

我认为你不希望使用数组,因为id永远不会匹配索引。所以我建议使用dictionary:

var markers = {};  // "{}" to specify dictionary instead of "[]" to specify array

创建标记时,将其添加到dictionary中:

markers[4] = marker;   // 4 is the id

当你想要得到它时:

var currentMarker = markers[4];