How can I remove an InfoWindow when the Marker is removed?

google-maps, google-maps-api-3, google-maps-markers, javascript

Solution

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

Problem

I have a Google Maps div and list of check boxes that I use to filter markers on the map. But if I click on a marker, open an `InfoWindow`, and then click on a check box to remove the markers of that type, the `InfoWindow` is not removed from the map. After I remove the markers, I call this code but the `InfoWindow` stays: ``` try { if( infowindow ) { infowindow.close(); } } catch(err) { } ```

Original source