Have just one InfoWindow open in Google Maps API v3

后端 未结 10 861
盖世英雄少女心
盖世英雄少女心 2020-11-28 03:18

I need to have only one InfoWindow open on my Google Map. I need to close all other InfoWindows before I open a new one.

Can someone show me how to do this?

相关标签:
10条回答
  • 2020-11-28 03:40

    Google Maps allows you to only have one info window open. So if you open a new window, then the other one closes automatically.

    0 讨论(0)
  • 2020-11-28 03:47

    a tad late, but I managed to have only one infowindow open by maken infowindow a global variable.

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

    then inside the listner

    infowindow.close();
    infowindow = new google.maps.InfoWindow({   
        content: '<h1>'+arrondissement+'</h1>'+ gemeentesFiltered                           
    });
    
    infowindow.open(map, this);
    
    0 讨论(0)
  • 2020-11-28 03:47

    Solved it this way:

    function window(content){
        google.maps.event.addListener(marker,'click', (function(){
            infowindow.close();
            infowindow = new google.maps.InfoWindow({
                content: content
            });
            infowindow.open(map, this);
        }))
    }
    window(contentHtml);
    
    0 讨论(0)
  • 2020-11-28 03:51

    I had the same problem but the best answer didn't solve it completely, what I had to do in my for statement was using the this relating to my current marker. Maybe this helps someone.

    for(var i = 0; i < markers.length; i++){
        name = markers[i].getAttribute("name");
        address = markers[i].getAttribute("address");        
        point = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));                                     
        contentString = '<div style="font-family: Lucida Grande, Arial, sans-serif;>'+'<div><b>'+ name +'</b></div>'+'<div>'+ address +'</div>';                    
        marker = new google.maps.Marker({                       
            map: map,
            position: point,
            title: name+" "+address,
            buborek: contentString 
        });                                     
        google.maps.event.addListener(marker, 'click', function(){
            infowindow.setContent(this.buborek); 
            infowindow.open(map,this); 
        });                                                         
        marker.setMap(map);                 
    }
    
    0 讨论(0)
提交回复
热议问题