一次只打开一个信息窗口 [英] Only open one infowindow at a time

查看:75
本文介绍了一次只打开一个信息窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建带有多个标记的Google地图.目前每个标记都有一个信息窗口,我怎么一次只能打开一个信息窗口?

I'm creating a Google map with multiple markers. At the moment each marker has an infowindow, how can I only have one infowindow open at a time?

此刻,它会在点击地图时关闭所有窗口.

At the moment it closes all windows on clicking the map.

我知道这个问题已经问过几次了,但是代码似乎和我的完全不同.

I know this question has been asked a few times but the code seems vastly different to mine.

// if marker contains HTML, add it to an infoWindow
if( $marker.html() )
{
    // create info window
    var infowindow = new google.maps.InfoWindow({
        content     : $marker.html()
    });

    // show info window when marker is clicked
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.open( map, marker );
    });

    google.maps.event.addListener(map, 'click', function(event) {
        if (infowindow) {
            infowindow.close(); }
        }); 

}

推荐答案

尝试以下操作:仅声明一个信息窗口(对于地图是全局的,而不是按标记的信息窗口).

Try this: declare just one infowindow (global to the map, not one by marker).

var infoWindow;
...
if( $marker.html() ){
    google.maps.event.addListener(marker, 'click', function() {
      if (infoWindow) infoWindow.close(); 
      infoWindow = new google.maps.InfoWindow({
         content     : $marker.html()
       });
       infoWindow.open( map, marker );
    });
}

这篇关于一次只打开一个信息窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆