如何更新标记的信息窗口?

How to update infowindow of a marker?

 for(i=0; i<locations.length; i++) {
        var position = new google.maps.LatLng(locations[i][2], locations[i][3]);
        var marker = new google.maps.Marker({
            position: position,
            map: map,
        });
        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                infowindow.setContent(locations[i][1]);
                infowindow.setOptions({maxWidth: 200});
                infowindow.open(map, marker);               
            }
        }) (marker, i));
        Markers[locations[i][4]] = marker;
    }

我的地图有多个带有信息窗口的标记我的目标是更新标记信息窗口但无法找到解决方案。

如果您尝试将 i 存储在闭包中,那么您可以这样做:Demo JsFiddle

  for (let i = 0; i < locations.length; i++) {
    let position = new google.maps.LatLng(locations[i][1], locations[i][2]);
    let marker = new google.maps.Marker({
      position: position,
      map: map,
      title: locations[i][0]
    });

    google.maps.event.addListener(marker, 'click', function (event) {
      let content = `<b>${locations[i][0]} </b><br>
            ${locations[i][3]}<br>
            Position: ${event.latLng.lat()}, ${event.latLng.lng()}`;

      infowindow.setContent(content);
      infowindow.setOptions({ maxWidth: 200 });
      infowindow.open(map, marker);
    });
  }

或者您可以从 event object event.latLng.lat() 获取位置并使用它获取位置。


或者你可以有单独的信息 window for each marker.