Google Maps API默认打开多个信息窗口

时间:2014-02-02 18:21:01

标签: javascript google-maps google-maps-api-2

默认情况下是否可以打开所有信息窗口。我尝试了以下但是它不起作用:

var infowindow = new google.maps.InfoWindow({
      maxWidth: 160
});

// Add the markers and infowindows to the map
for (var 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: icons[iconCounter]
    });

    infowindow.setContent(locations[i][0]);
    infowindow.open(map, marker);
}

1 个答案:

答案 0 :(得分:9)

您的代码只包含一个infowindow,如果您希望 all 打开,则需要为每个标记创建一个infowindow。

更新:在我写这篇文章时没有注意到这个问题被标记为google-maps-api-2。此答案仅适用于Google Maps Javascript API v3the deprecated Google Maps Javascript API v2,一次只支持一个信息窗口。

// Add the markers and infowindows to the map
for (var i = 0; i < locations.length; i++) {
    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map,
        icon: icons[iconCounter]
    });

    var infowindow = new google.maps.InfoWindow({
      content: locations[i][0],
      maxWidth: 160
    });
    infowindow.open(map, marker);
}
相关问题