gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:26:36 26 4
gpt4 key购买 nike

如何让所有信息窗口在单击另一个图钉或单击 map 本身时关闭?我正在使用 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);
});

最佳答案

正如您在 API docs 中看到的那样, InfoBox 有一个 close() 方法。

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

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

jQuery(document).ready(function ($) {
var infoWindows = [];

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

//add infowindow to array
infoWindows.push(infowindow);

添加这个函数

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

这里通过链接调用

<a href="#" onclick="closeAllInfoWindows();">Close all infowindows</a>

关于javascript - 关闭所有信息窗口谷歌地图 API V3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19067027/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com