Have just one InfoWindow open in Google Maps API v3

后端 未结 10 896
盖世英雄少女心
盖世英雄少女心 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:31

    Declare a globar var selectedInfoWindow; and use it to hold the opened info window:

    var infoWindow = new google.maps.InfoWindow({
        content: content
    });
    
    // Open the infowindow on marker click
    google.maps.event.addListener(marker, "click", function() {
        //Check if there some info window selected and if is opened then close it
        if (selectedInfoWindow != null && selectedInfoWindow.getMap() != null) {
            selectedInfoWindow.close();
            //If the clicked window is the selected window, deselect it and return
            if (selectedInfoWindow == infoWindow) {
                selectedInfoWindow = null;
                return;
            }
        }
        //If arrive here, that mean you should open the new info window 
        //because is different from the selected
        selectedInfoWindow = infoWindow;
        selectedInfoWindow.open(map, marker);
    });
    

提交回复
热议问题