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

vim*_*984 15 javascript google-maps google-maps-api-3

如何在关闭另一个引脚或单击地图时关闭所有信息窗口?我正在使用http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/docs/reference.html 和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);
});
Run Code Online (Sandbox Code Playgroud)

dav*_*rad 54

正如您在API文档中看到的那样,InfoBox具有close()-method.

收集您在阵列中创建的所有InfoBox.然后迭代这个数组并调用close每个信息框,当你需要立即关闭它们时.

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

jQuery(document).ready(function ($) {
    var infoWindows = [];
Run Code Online (Sandbox Code Playgroud)

在功能中,showInContentWindow在之后添加以下内容var infowindow=new..,例如在之前infowindow.open

//add infowindow to array
infoWindows.push(infowindow); 
Run Code Online (Sandbox Code Playgroud)

添加此功能

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

这里通过链接调用

<a href="#" onclick="closeAllInfoWindows();">Close all infowindows</a>
Run Code Online (Sandbox Code Playgroud)

  • 但是,如果信息窗口用户可能已经打开了再见谷歌的地方? (3认同)

Dan*_*Mai 10

您还可以将活动(打开)信息窗口保持在更高的范围或全局变量中

var activeInfoWindow;
Run Code Online (Sandbox Code Playgroud)

在click事件监听器中,关闭活动信息窗口(如果有),然后将this信息窗口设置为活动状态

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

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

  • 如果您一次只打开1x infowindow,这是最佳解决方案 (2认同)