Javascript 在谷歌地图上用多个信息窗口打印多个标记

Javascript 在谷歌地图上用多个信息窗口打印多个标记,javascript,arrays,loops,dynamic,google-maps-api-3,Javascript,Arrays,Loops,Dynamic,Google Maps Api 3,我有一个mapArray,它是根据我们数据库中的客户端设施动态创建的 我试图循环浏览它们,并创建一个单击侦听器,分别打开每个侦听器的信息窗口 这是我到目前为止所拥有的 for (var i in mapArray) { var thislatlng = new google.maps.LatLng(mapArray[i][1], mapArray[i][2]), contentString = '<b>' + mapArray[i][6] + '</b>

我有一个
mapArray
,它是根据我们数据库中的客户端设施动态创建的

我试图循环浏览它们,并创建一个
单击
侦听器,分别打开每个侦听器的信息窗口

这是我到目前为止所拥有的

for (var i in mapArray) {

    var thislatlng = new google.maps.LatLng(mapArray[i][1], mapArray[i][2]),
    contentString = '<b>' + mapArray[i][6] + '</b><br /><br />' + mapArray[i][3] + '<br /><br /><a href="http://maps.google.com/maps?daddr=' + mapArray[i][3].replace("<br />", " ").replace("#", " ") + '" target ="_blank">Get Directions<\/a>',
    marker = new google.maps.Marker({
        map: map,
        position: thislatlng
    }),
    infowindow = new google.maps.InfoWindow({
        content: contentString
    });

    google.maps.event.addListener(marker, 'click', function() {

        infowindow.open(map, this);
    });
}

但这仍然不能解决窗口问题


如何使用“动态打开各自的
信息窗口”
?它们只是取最后一个的值。

JavaScript没有块作用域<代码>信息窗口
被拉到函数顶部,每个标记都指向同一个,这是最后一个实例化的窗口。您可以重复使用同一个信息窗口,确保不会同时打开多个信息窗口,并根据单击的标记替换内容:

var infowindow = new google.maps.InfoWindow();

for (var i in mapArray) {

    var thislatlng = new google.maps.LatLng(mapArray[i][1], mapArray[i][2]),
        contentString = '<b>' + mapArray[i][6] + '</b><br /><br />' + mapArray[i][3] + '<br /><br /><a href="http://maps.google.com/maps?daddr=' + mapArray[i][3].replace("<br />", " ").replace("#", " ") + '" target ="_blank">Get Directions<\/a>',
        marker = new google.maps.Marker({
            map: map,
            position: thislatlng,
            information: contentString //Just store the string in a property
        });

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(this.information); //Recall the property when clicked to set the content of the window
        infowindow.open(map, this);
    });
}
var infowindow=new google.maps.infowindow();
for(mapArray中的变量i){
var thislatlng=new google.maps.LatLng(mapArray[i][1],mapArray[i][2]),
contentString=''+mapArray[i][6]+'

'+mapArray[i][3]+'

infowindow.open(map, this);
var infowindow = new google.maps.InfoWindow();

for (var i in mapArray) {

    var thislatlng = new google.maps.LatLng(mapArray[i][1], mapArray[i][2]),
        contentString = '<b>' + mapArray[i][6] + '</b><br /><br />' + mapArray[i][3] + '<br /><br /><a href="http://maps.google.com/maps?daddr=' + mapArray[i][3].replace("<br />", " ").replace("#", " ") + '" target ="_blank">Get Directions<\/a>',
        marker = new google.maps.Marker({
            map: map,
            position: thislatlng,
            information: contentString //Just store the string in a property
        });

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(this.information); //Recall the property when clicked to set the content of the window
        infowindow.open(map, this);
    });
}