How can I remove InfoWindow when deleting a marker?

I have a Google Maps div and a list of checkboxes that I use to filter markers on the map.

But if I click a marker, open InfoWindowand then check the box to remove markers of this type, InfoWindowit is not deleted from the map.

After removing the markers, I call this code, but it InfoWindowremains:

try {
    if( infowindow ) {
          infowindow.close();
    }
}
catch(err) { }
+3
source share
2 answers

I suggest you change the code that creates the click listener Markerthat your InfoWindow will open by adding code similar to the following example:

google.maps.event.addListener( marker, "click", function() {
    var bubble = new google.maps.InfoWindow({
        content: buildBubbleContent( param1, param2 )
    });
    bubble.open( map, marker );
    //pretty standard stuff to here, but the next line is new (for me):
    google.maps.event.addListenerOnce( marker, "visible_changed", function() {
        bubble.close();
    });
});

As discussed in the question: How to clear InfoWindow when the associated token is hidden? :

+5
function closeInfoWindow() {
        if (infoWindow !== null) {
            google.maps.event.clearInstanceListeners(infoWindow);  // just in case handlers continue to stick around
            infoWindow.close();
            infoWindow = null;
        }
    }
+4

All Articles