Show all infowindows open

2020-04-14 09:11发布

I'm trying to have custom infowindows float above markers, however I noticed that only one marker can be opened at any one time. Is there a workaround to this?

Here's the code I have produced at the moment:

downloadUrl("AllActivityxml.php", function(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
        var name = markers[i].getAttribute("id");
        var address = markers[i].getAttribute("id");
        var type = markers[i].getAttribute("venue_type");
        var point = new google.maps.LatLng(
            parseFloat(markers[i].getAttribute("lat")),
            parseFloat(markers[i].getAttribute("lng"))
        );

        var infowindow = new google.maps.InfoWindow();
        var html = "<b>" + point + "</b>hello <br/>" + type;
        var icon = customIcons[type] || {};
        var marker = new google.maps.Marker({
            map: map,
            position: point,
            icon: icon.icon,
            shadow: icon.shadow,
            zIndex: Math.round(latlng.lat()*-100000)<<5
        });

        markersArray.push(marker);
        bindInfoWindow(marker, map, infoWindow, html);        


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

And when you check this map, notice that I cannot open more than 2 infowindows at once. Why is that?

1条回答
ゆ 、 Hurt°
2楼-- · 2020-04-14 09:29

There is no limitation implicit to the Google Maps API v3 that makes only one InfowWindow available at a time. You need to write your code to do that. If you want an InfoWindow for each marker, make one.

Some thing like (not tested):

function createMarker(latlng, html) {
   var contentString = html;
    var infowindow = new google.maps.InfoWindow();
    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        zIndex: Math.round(latlng.lat()*-100000)<<5
        });

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(contentString); 
        infowindow.open(map,marker);
        });
}
查看更多
登录 后发表回答