本文介绍了google maps api v3-定位标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样设置我的标记

    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]
      });

      markers.push(marker);

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

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



}
}

这似乎仅将事件处理程序附加到最后一个.如何将其添加到所有标记中

This only seems to attach the event handler on the last one. How do I add it to all markers

推荐答案

您需要将addListener包裹在一个闭包中.

You need to wrap your addListener in a closure.

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);
}

这篇关于google maps api v3-定位标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 16:38