删除标记后如何删除InfoWindow?

时间:2012-04-28 11:55:29

标签: javascript google-maps google-maps-api-3 google-maps-markers

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

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

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

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

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:5)

我建议您更改创建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();
    });
});

正如所讨论的那样:How do I clean up an InfoWindow when the associated Marker is hidden?