javascript 删除标记后如何删除信息窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10363232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:36:47  来源:igfitidea点击:

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

javascriptgoogle-mapsgoogle-maps-api-3google-maps-markers

提问by 1110

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

我有一个 Google Maps div 和用于过滤地图上标记的复选框列表。

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 InfoWindowis not removed from the map.

但是,如果我单击一个标记,打开一个InfoWindow,然后单击一个复选框以删除该类型的标记,InfoWindow则不会从地图中删除。

After I remove the markers, I call this code but the InfoWindowstays:

删除标记后,我调用此代码,但InfoWindow保持不变:

try {
    if( infowindow ) {
          infowindow.close();
    }
}
catch(err) { }

回答by Baz1nga

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

回答by Sean Mickey

I suggest that you change the code that creates the Markerclick listener that opens your InfoWindow by adding code similar to the following example:

我建议Marker您通过添加类似于以下示例的代码来更改创建用于打开您的 InfoWindow的单击侦听器的代码:

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 question: How do I clean up an InfoWindow when the associated Marker is hidden?:

正如问题所讨论的:当隐藏关联的标记时,如何清理信息窗口?