-1

我正在为我的网站制作谷歌地图,以便您可以查看每个用户的位置。仅当您单击某人的图标时,这才完美工作,每次我无法理解时,它都会向您显示相同的内容我做错了什么吗?

    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET","includes/xml.php",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML; 

    var x=xmlDoc.getElementsByTagName("USER");
    var infoWindow = new google.maps.InfoWindow();
    var map; 
    var users = [];
    function initialize() { 
      var mapOptions = {
      zoom: 9, 
      center: new google.maps.LatLng(52.1424, 5.09428), 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
    };
    map = new google.maps.Map(document.getElementById('map-canvas'),
    mapOptions);

    for (i=0;i<x.length;i++) { 
      var id = x[i].getElementsByTagName("USERID")[0].childNodes[0].nodeValue;
      users['USERNAME'+id] = x[i].getElementsByTagName("USERNAME")[0].childNodes[0].nodeValue;
      users['CITY'+id] = x[i].getElementsByTagName("CITY")[0].childNodes[0].nodeValue;
      users['COUNTRY'+id] = x[i].getElementsByTagName("COUNTRY")[0].childNodes[0].nodeValue;
      users['IMAGE'+id] = x[i].getElementsByTagName("IMAGE")[0].childNodes[0].nodeValue;
      users['IMAGEPIN'+id] = x[i].getElementsByTagName("IMAGEPIN")[0].childNodes[0].nodeValue;
      users['LAT'+id] = x[i].getElementsByTagName("LAT")[0].childNodes[0].nodeValue;
      users['LONG'+id] = x[i].getElementsByTagName("LONG")[0].childNodes[0].nodeValue;
      users['DATETIME'+id] = x[i].getElementsByTagName("DATETIME")[0].childNodes[0].nodeValue;
      users['TWITTER'+id] = x[i].getElementsByTagName("TWITTER")[0].childNodes[0].nodeValue;
      users['FA'+id] = x[i].getElementsByTagName("FA")[0].childNodes[0].nodeValue;
      users['ACTIVEUSER'+id] = x[i].getElementsByTagName("ACTIVEUSER")[0].childNodes[0].nodeValue;
      users['myLatlng'+id] = new google.maps.LatLng(users['LAT'+id],users['LONG'+id]);
      users['content'+id] = '<div style="width:220px;"><img id="avatar"style="margin-right: 5px;" src="' + users['IMAGE'+id] + '"></img>' + 
        '<b>' + users['USERNAME'+id] + '</b><br />' + 
        users['CITY'+id] + '<br />' + users['COUNTRY'+id] + '<br />' + 
        '<i>' + users['DATETIME'+id] + '</i><br />' +
        '<a href="http://www.furaffinity.net/user/' + users['FA'+id] + '" target="_blank"><img src="http://www.kinzart.com/images/fur-affinity-icon.png" Alt="Twitter" width="30" height="30"></a>' +
        '<a href="http://twitter.com/' + users['TWITTER'+id] + '/" target="_blank"><img src="http://biophiliccities.org/wp-content/uploads/2013/06/twitterICON.png" Alt="Twitter" width="30" height="30"></a>';
      users['window'+id] = new google.maps.InfoWindow({ content: users['content'+id]});
      users['marker'+id] = new google.maps.Marker({ position: users['myLatlng'+id], map: map, title: users['USERNAME'+id], icon: users['IMAGEPIN'+id]});
      google.maps.event.addListener(users['marker'+id], 'click', function() { infoWindow.setContent(users['content'+id]); infoWindow.open(map, users['marker'+id]) });
      }
    }
    google.maps.event.addDomListener(window, 'load', initialize);
4

1 回答 1

1

问题是,当您在循环中设置事件侦听器时,最终循环的每次迭代都会将 infowindow 内容设置为最后一个值users['content'+id]。您需要考虑在代码中使用闭包。

这是另一种应该可行的方法:

for (i=0;i<x.length;i++) { 
      ...  // trimmed for brevity
      users['marker'+id] = new google.maps.Marker({ position: users['myLatlng'+id], map: map, title: users['USERNAME'+id], icon: users['IMAGEPIN'+id]});
      bindInfoWindow(users['marker'+id], users['content'+id]);
}

然后有这个功能:

function bindInfoWindow(marker, content) {
    google.maps.event.addListener(marker, 'click', function() { 
        infoWindow.setContent(content); 
        infoWindow.open(map, marker) 
    });
}
于 2013-10-20T17:32:56.773 回答