如何在显示多个标记时单击另一个标记时关闭信息窗口

How to close InfoWindow when clicking on another marker when multiple markers are displayed

本文关键字:信息 信息窗 窗口 另一个 单击 显示      更新时间:2023-09-26

我想在点击下一个标记时关闭当前信息窗口,因为现在它会在点击后显示所有信息窗口。

我的JavaScript

var marker = new MarkerWithLabel({
    position: latlng,
    draggable: false,
    raiseOnDrag: true,
    map: map,
    icon: 'a.png',
    labelContent: '<?php echo $rating.' % Review '.$review;?>',
    labelAnchor: new google.maps.Point(22, 0),
    labelClass: "labels"
});
marker['infowindow'] = new google.maps.InfoWindow({
    content: '<p><?php echo $name ;?></p>'
});

google.maps.event.addListener(marker, 'click', function() {
    this['infowindow'].open(map, this);
});

将以前打开的信息窗口保存在变量中,然后在打开新窗口时关闭。

var currWindow =false; 
base.attachInfo = function(marker, i){
    var infowindow = new google.maps.InfoWindow({
        content: "Content"
    });
    google.maps.event.addListener(marker, 'click', function(){
        if( currWindow ) {
           currWindow.close();
        }
        currWindow = infowindow;
        infowindow.open(base.map, marker);
    });
}