0

我正在使用 google maps api v3 开发一个项目,并且正在尝试制作它,以便在单击标记时出现一个信息窗口。我已经完成了这一点,但现在我想要的是当点击不同的标记时显示不同的内容,我似乎无法弄清楚。任何帮助表示赞赏。谢谢!

    function initialize() {

var locations = [
      ['Phi Delta Theta', 39.511747,-84.735117],
      ['Pi Kappa Alpha', 39.511776, -84.735684],
      ['Sigma Nu', 39.513921, -84.735159],
      ['Sigma Alpha Epsilon', 39.514332, -84.734956],
      ['Beta Theta Pi', 39.5107519, -84.738549]
    ];

var myLatlng = new google.maps.LatLng(39.511747,-84.735117);
var mapOptions = {
  zoom: 15,
  center: myLatlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
}

var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

var contentString = '<div id="content">'+

    '</div>';

var infowindow = new google.maps.InfoWindow({
    content: contentString
});

var marker, i;

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

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
      }


google.maps.event.addListener(marker, 'click', function() {
  infowindow.open(map,marker);
});



      }
4

1 回答 1

0

这是您想要的答案,但是它允许一次打开多个 infoWindow。它的重点是定义windowinfo的多个实例。代替:

var infowindow = new google.maps.InfoWindow({
    content: contentString
});
....
for (i = 0; i < locations.length; i++) {

}

这样做:

for (i = 0; i < locations.length; i++) {
    var infowindow = new google.maps.InfoWindow({
        content: contentString
    });
}

这样,您将定义多个标记,每个标记都有不同的内容。

于 2013-04-13T14:11:27.693 回答