Google Maps: Auto close open InfoWindows?

前端 未结 12 647
终归单人心
终归单人心 2020-12-02 05:30

On my site, I\'m using Google Maps API v3 to place house markers on the map.

The InfoWindows stay open unless you explicitly click the close icon. Meaning, you can h

相关标签:
12条回答
  • 2020-12-02 06:22
    var map;
    var infowindow;
    ...
    function createMarker(...) {
        var marker = new google.maps.Marker({...});
        google.maps.event.addListener(marker, 'click', function() {
            ...
            if (infowindow) {
                infowindow.close();
            };
            infowindow = new google.maps.InfoWindow({
                content: contentString,
                maxWidth: 300
            });
            infowindow.open(map, marker);
        }
    ...
    function initialize() {
        ...
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        ...
        google.maps.event.addListener(map, 'click', function(event) {
            if (infowindow) {
                infowindow.close();
            };
            ...
        }
    }
    
    0 讨论(0)
  • 2020-12-02 06:28

    From this link http://www.svennerberg.com/2009/09/google-maps-api-3-infowindows/:

    Teo: The easiest way to do this is to just have one instance of the InfoWindow object that you reuse over and over again. That way when you click a new marker the infoWindow is “moved” from where it’s currently at, to point at the new marker.

    Use its setContent method to load it with the correct content.

    0 讨论(0)
  • 2020-12-02 06:30

    alternative solution for this with using many infowindows: save prev opened infowindow in a variable and then close it when new window opened

    var prev_infowindow =false; 
    ...
    base.attachInfo = function(marker, i){
        var infowindow = new google.maps.InfoWindow({
            content: 'yourmarkerinfocontent'
        });
    
        google.maps.event.addListener(marker, 'click', function(){
            if( prev_infowindow ) {
               prev_infowindow.close();
            }
    
            prev_infowindow = infowindow;
            infowindow.open(base.map, marker);
        });
    }
    
    0 讨论(0)
  • 2020-12-02 06:31

    How about -

    google.maps.event.addListener(yourMarker, 'mouseover', function () {
            yourInfoWindow.open(yourMap, yourMarker);
    
        });
    
    google.maps.event.addListener(yourMarker, 'mouseout', function () {
            yourInfoWindow.open(yourMap, yourMarker);
    
        });
    

    Then you can just hover over it and it will close itself.

    0 讨论(0)
  • 2020-12-02 06:34

    My solution.

    var map;
    var infowindow = new google.maps.InfoWindow();
    ...
    function createMarker(...) {
    var marker = new google.maps.Marker({
         ...,
         descrip: infowindowHtmlContent  
    });
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setOptions({
            content: this.descrip,
            maxWidth:300
        });
        infowindow.open(map, marker);
    });
    
    0 讨论(0)
  • 2020-12-02 06:36

    Declare a variable for active window

    var activeInfoWindow; 
    

    and bind this code in marker listener

     marker.addListener('click', function () {
        if (activeInfoWindow) { activeInfoWindow.close();}
        infowindow.open(map, marker);
        activeInfoWindow = infowindow;
    });
    
    0 讨论(0)
提交回复
热议问题