gpt4 book ai didi

api - Google API V3 多个信息窗口加上点击关闭

转载 作者:行者123 更新时间:2023-12-01 09:33:19 25 4
gpt4 key购买 nike

我想出了如何在信息窗口中使用多个标记,但是当您单击另一个标记时它们不会关闭,我相信这是因为我正在为每个标记创建一个新的信息窗口,任何帮助将不胜感激。

    <script type="text/javascript">


var map ;
function initialize() {
var latlng = new google.maps.LatLng(53.063165, -3.205390);
var myOptions = {
zoom: 6,
center: latlng,
mapTypeId: google.maps.MapTypeId.SATELLITE,
zoomControl: false,
mapTypeControl: false,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
position: google.maps.ControlPosition.LEFT_CENTER
},
panControl: false,
streetViewControl: false,
streetViewControlOptions: {
position: google.maps.ControlPosition.LEFT_CENTER
}
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

var marker = add_marker(56.747923,-3.717155,"Ben Vrackie","<b>Ben Vrackie</b><br><br>Perthshire classic! some sweet hidden gems on this loop.<br> surrounded by amazing countryside, Lochs, hills this<br>has it all... Red Grade."); // pass in as Latitude, then Longitude
marker.setMap(map);

var marker = add_marker(57.556366,-5.409222,"Torridon Forest","<b>Torridon Forest</b><br><br>Technical and breathtaking.... <br>Black/red grade."); // pass in as Latitude, then Longitude
marker.setMap(map)
}


function add_marker(lat,lng,title,box_html) {

var infowindow = new google.maps.InfoWindow({
content: box_html
});

var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
map: map,
title: title
});


google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
return marker;
}



</script>

最佳答案

只创建一个全局 InfoWindow 对象。

 //Global 
var infowindow = new google.maps.InfoWindow();

然后

    function add_marker(lat,lng,title,box_html) {

var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
map: map,
title: title
});

google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(box_html);

infowindow.open(map,marker);
});
return marker;
}

关于api - Google API V3 多个信息窗口加上点击关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12567280/

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