Google Maps API open multiple info windows by default

泄露秘密 提交于 2019-11-30 10:00:26

问题


Is it possible to open all info windows by default. I tried the following but it doesn't work:

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:


Your code only includes one infowindow, if you want them all to open, you need to create an infowindow for each marker.

Update: didn't notice when I wrote this that this question is tagged google-maps-api-2. This answer will only work for the Google Maps Javascript API v3, the deprecated Google Maps Javascript API v2, only supports a single infowindow at a time.

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


来源:https://stackoverflow.com/questions/21514388/google-maps-api-open-multiple-info-windows-by-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!