javascript 谷歌MapAPI v3 -目标标记

2skhul33  于 2023-01-19  发布在  Java
关注(0)|答案(1)|浏览(133)

我把我的记号笔设成这样

var
  marker,
  i,
  markers = [];

for (i = 0; i < locations.length; i++) {
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map,
    icon: 'img/markers/t1.png',
    id: locations[i][3]
  });

  markers.push(marker);

  marker.addListener('mouseover', function() {

    marker.setIcon("img/serve-bracket.png");
  });
}

这似乎只附加了最后一个事件处理程序。我如何将它添加到所有标记

j5fpnvbx

j5fpnvbx1#

您需要将addListener封装在闭包中。

var marker, i;
var markers = [];

for (i = 0; i < locations.length; i++) {
    marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map,
        icon: 'img/markers/t1.png',
        id: locations[i][3]
    });

    google.maps.event.addListener(marker, 'mouseover', (function(marker, i) {
        return function() {
            marker.setIcon("img/serve-bracket.png");
        }
    })(marker, i));

    markers.push(marker);
}

相关问题