【发布时间】:2019-03-14 17:21:38
【问题描述】:
是否有人对单击另一个标记时如何自动关闭信息窗口有任何建议?当前,用户必须在单击另一个标记时手动关闭信息框。我希望有一种方法可以轻松地合并到我的代码中。看起来我正在为每个标记创建单独的信息框。当我们扩大这个项目的规模时,我们将拥有 10k 多个标记,这些标记将被不同的类别分开。
我阅读了有关创建一个 infoWindow 然后更改内容的建议,但如果我正确理解我的代码,我将为每个标记创建单独的信息框,因为它们来自 XML 文件。我如何将这种技术与我当前的代码结合起来?
您可以在此处找到我的项目的实时示例:http://www.officerage.com/maps/ 这是我的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<meta charset="UTF-8">
<title>A Basic Map</title>
<style>
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
function infoCallback(infowindow, marker) {
return function() { infowindow.open(map, marker); };
}
function initMap() {
var mapOptions = {
zoom: 3,
center: new google.maps.LatLng(37.5, -122),
mapTypeId: 'roadmap'
};
var map = new google.maps.Map(document.getElementById('map'), mapOptions);
$.ajax({
type: "GET",
async: true,
url: "testxml.xml",
dataType: "xml",
success:
function (xml) {
var places = xml.documentElement.getElementsByTagName("place");
for (var i = 0; i < places.length; i++) {
var lat = places[i].getAttribute('latitude');
var long = places[i].getAttribute('longitude');
var category = places[i].getAttribute('Info');
var latLng = new google.maps.LatLng(lat, long);
var marker = new google.maps.Marker({
position: latLng,
map: map,
category:places[i].info,
label:places[i].name
});
var infowindow = new google.maps.InfoWindow({
content:category});
google.maps.event.addListener(marker, 'click', infoCallback(infowindow,marker));
}
}
}
);
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCz_-TAwsP-dL_dsEkmz8sGEU1hyPYKHik&callback=initMap">
</script>
</body>
</html>
【问题讨论】:
标签: google-maps google-maps-api-3