Javascript 关闭所有信息窗口谷歌地图API V3?

Javascript 关闭所有信息窗口谷歌地图API V3?,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,如何在点击另一个pin或点击地图本身时关闭所有信息窗口? 我正在使用 和kml覆盖。 以下是我迄今为止的JS: jQuery(document).ready(function ($) { function initialize() { google.maps.visualRefresh = true; var myLatlng = new google.maps.LatLng(51.201465, -0.30244); var mapOpt

如何在点击另一个pin或点击地图本身时关闭所有信息窗口? 我正在使用 和kml覆盖。 以下是我迄今为止的JS:

jQuery(document).ready(function ($) {
    function initialize() {
        google.maps.visualRefresh = true;
        var myLatlng = new google.maps.LatLng(51.201465, -0.30244);
        var mapOptions = {
            zoom: 12,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        var map = new google.maps.Map(document.getElementById('map-canvas'),
        mapOptions);

        var kmlLayer = new google.maps.KmlLayer({
            url: 'http://***.com/new/wp-content/themes/required-starter/CGAGolfAcademies.kml?rand=' + (new Date()).valueOf(),
            suppressInfoWindows: true,
            map: map
        });

        google.maps.event.addListener(kmlLayer, 'click', function (kmlEvent) {
            showInContentWindow(kmlEvent.latLng, kmlEvent.featureData.description);
        });

        function showInContentWindow(position, text) {
            var content = "<div class='info_win'><p>" + text + "</p></div>";
            var infowindow =new InfoBox({
                 content: content,
                 disableAutoPan: false,
                 maxWidth: 0,
                 position: position,
                 pixelOffset: new google.maps.Size(-140, 0),
                 zIndex: null,
                 boxStyle: { 
                  background: "#FBFBFB"
                  ,opacity: 0.90
                  ,width: "280px"
                 },
                 closeBoxMargin: "10px 2px 2px 2px",
                 closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif",
                 infoBoxClearance: new google.maps.Size(1, 1),
                 isHidden: false,
                 pane: "floatPane",
                 enableEventPropagation: false
        });


    infowindow.open(map);

        }
                    /******AJAX MAP ****/
            siteURL = 'http://' + top.location.host.toString();
            coachesLinks = jQuery('.info_win a');
            coachesLinks.click(function (e) {
                e.preventDefault();
            });
            coachesLinks.click(function (e) {
                alert('FRED');
                $el = jQuery(this);
                URL = $el.attr('href');
                shareurl = $el.attr('href');
                URL = URL + " .main";
                jQuery('#content_two').show('slow').load(URL, function () {
                    scrollToAnchor('content_two');
                    $('.main').css('overflow', 'visible');
                    $('#content_two').css('overflow', 'visible');
                    jQuery('#content_two .back').on('click', function () {
                        jQuery(this).hide('slow');
                        var contentTwo = jQuery('#content_two');
                        if (contentTwo.is(':hidden')) {
                            jQuery('#content_two .back').hide();
                        } else {
                            contentTwo.hide('slow');
                            jQuery('#content > .main').show('slow');
                            jQuery('#content > .main').css('overflow', 'visible');
                            scrollToAnchor('access');
                        }
                    });

                });
                $('#content > .main').hide('slow');
            });

    }

    google.maps.event.addDomListener(window, 'load', initialize);
});
jQuery(文档).ready(函数($){
函数初始化(){
google.maps.visualRefresh=true;
var mylatng=new google.maps.LatLng(51.201465,-0.30244);
变量映射选项={
缩放:12,
中心:myLatlng,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById('map-canvas'),
地图选项);
var kmlLayer=new google.maps.kmlLayer({
url:'http://***.com/new/wp content/themes/required starter/CGAGolfAcademies.kml?rand='+(新日期()).valueOf(),
suppressInfoWindows:对,
地图:地图
});
google.maps.event.addListener(Kmlayer,'click',函数(kmlEvent){
showInContentWindow(kmlEvent.latLng、kmlEvent.featureData.description);
});
函数showInContentWindow(位置、文本){
var content=“”+文本+”

”; var infowindow=新信息框({ 内容:内容,, disableAutoPan:错, 最大宽度:0, 职位:职位,, pixelOffset:new google.maps.Size(-140,0), zIndex:null, boxStyle:{ 背景:“FBFB” ,不透明度:0.90 ,宽度:“280px” }, closeBoxMargin:“10px 2px 2px 2px”, closeBoxURL:“http://www.google.com/intl/en_us/mapfiles/close.gif", infoBoxClearance:new google.maps.Size(1,1), 伊希登:错, 窗格:“浮动窗格”, enableEventPropagation:false }); 打开(地图); } /******AJAX映射****/ siteURL='http://'+top.location.host.toString(); coachesLinks=jQuery('.info_win a'); coachesLinks.单击(函数(e){ e、 预防默认值(); }); coachesLinks.单击(函数(e){ 警惕(“弗雷德”); $el=jQuery(this); URL=$el.attr('href'); shareurl=$el.attr('href'); URL=URL+“.main”; jQuery('#content_two')。show('slow')。load(URL,函数(){ 滚动到anchor('content_two'); $('.main').css('overflow','visible'); $('content'u two').css('overflow','visible'); jQuery('#content_two.back')。on('click',function(){ jQuery(this.hide('slow'); var contentTwo=jQuery(“#content_two”); if(contentTwo.is(':hidden')){ jQuery('#content_two.back').hide(); }否则{ contentTwo.hide('slow'); jQuery('#content>.main').show('slow'); jQuery('#content>.main').css('overflow','visible'); scrollToAnchor(“访问”); } }); }); $('#content>.main')。隐藏('slow'); }); } google.maps.event.addDomListener(窗口“加载”,初始化); });
正如您在中看到的,信息框有一个
close()
-方法

收集您在数组中创建的所有信息框。然后迭代该数组,并在需要同时关闭所有信息框时,为每个信息框调用
close

在顶部,添加一个数组以保存创建的所有信息框

jQuery(document).ready(function ($) {
    var infoWindows = [];
在函数
showincontinwindow
中,在
var infowindow=new..
之后添加以下内容,例如在
infowindow之前。打开

//add infowindow to array
infoWindows.push(infowindow); 
添加此功能

function closeAllInfoWindows() {
  for (var i=0;i<infoWindows.length;i++) {
     infoWindows[i].close();
  }
}
函数closeAllInfo窗口(){

对于(var i=0;i,您还可以将活动(打开的)信息窗口保持在更高的范围或全局变量中

var activeInfoWindow;
在click event listener中,关闭活动信息窗口(如果有),然后将
this
info窗口设置为活动

var infoWindow = new google.maps.InfoWindow({
  content: name
});

google.maps.event.addListener(marker, 'click', function() {
  activeInfoWindow&&activeInfoWindow.close();
  infoWindow.open(map, marker);
  activeInfoWindow = infoWindow;
});

如果有人想在gmaps3 jQuery包装器的上下文中这样做

var infoWindows = [];
var locations=[
    {position:[123,456],content:'<h3>marker title 1</h3><p>msg text</p>/div>'}
]
var map=$('#mapName').gmap3()
.marker(locations)
.infowindow(locations)
.then(function (infowindow) {
    var map = this.get(0);
    var marker = this.get(1);
    marker.forEach(function(item,i){
        item.addListener('click', function() {
            closeAllInfoWindows();
            infowindow[i].open(map, item);
            infoWindows.push(infowindow[i]);
        });
    })
})
.fit();

function closeAllInfoWindows() {
  for (var i=0;i<infoWindows.length;i++) {
     infoWindows[i].close();
  }
}
var infoWindows=[];
变量位置=[
{位置:[123456],内容:'marker title 1msg text

/div>'} ] var map=$('#mapName').gmap3() .标记(位置) .信息窗口(位置) .then(功能(信息窗口){ var map=this.get(0); var marker=this.get(1); marker.forEach(功能(项目一){ item.addListener('单击',函数()){ 关闭所有信息窗口(); 信息窗口[i]。打开(地图、项目); 推送(infowindow[i]); }); }) }) .fit(); 函数closeAllInfo Windows(){
对于(var i=0;i但windows用户可能通过单击google places打开的信息如何?很好的解决方案。谢谢!如果您一次只打开1个信息窗口,这是最好的解决方案,可能是重复的
var infoWindows = [];
var locations=[
    {position:[123,456],content:'<h3>marker title 1</h3><p>msg text</p>/div>'}
]
var map=$('#mapName').gmap3()
.marker(locations)
.infowindow(locations)
.then(function (infowindow) {
    var map = this.get(0);
    var marker = this.get(1);
    marker.forEach(function(item,i){
        item.addListener('click', function() {
            closeAllInfoWindows();
            infowindow[i].open(map, item);
            infoWindows.push(infowindow[i]);
        });
    })
})
.fit();

function closeAllInfoWindows() {
  for (var i=0;i<infoWindows.length;i++) {
     infoWindows[i].close();
  }
}