如何在显示多个标记时单击另一个标记时关闭InfoWindow

时间:2016-05-03 12:33:14

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

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

我的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);
});

1 个答案:

答案 0 :(得分:3)

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

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);
    });
}