我想在Google地图上提供一些城市的基本信息。这个想法是,当人们打开地图时,他们会看到已打开的信息,而不必点击每个标记。然而,我无法弄清楚这是否可能。我已经浏览了所有样本,并没有看到这个被雇用。这样的选择是否存在?我附上了我的意图的视觉样本。
答案 0 :(得分:1)
您可以遍历所有Marker对象并调用setVisible(true)吗?这就是我要尝试的(我只熟悉API的V3),例如。
var marker = new google.maps.Marker({map: map, position:map.getCenter()});
marker.setVisible(true);
以上示例显示了如何为单个Marker执行此操作,但您可以对所有Markers执行此操作(可能将它们保存在数据结构中并遍历它们)
答案 1 :(得分:1)
只需在初始化时迭代标记数据,然后根据标记位置创建信息窗口。
<html>
<head>
<script type="text/javascript" src= "http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
var map;
//this json contains your raw data as well as space to store created marker and infowindow objects
info = [{
"content": "content 1",
"coords":[-34.397, 150.644],
"marker":null,
"infowindow": null
},{
"content": "content 2",
"coords":[-34.597, 150.644],
"marker":null,
"infowindow": null
}]
function initialize() {
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
for(i=0; i<info.length; i++) {
//create markers
pos = new google.maps.LatLng(info[i].coords[0],info[i].coords[1])
marker = new google.maps.Marker({
position: pos,
map: map
});
//store the marker object for later if you want, useful if you want to keep track of your objects or dynamically alter your objects
info[i].marker = marker
infowindow = new google.maps.InfoWindow({
content: info[i].content
});
//store info window object for later if you want, useful if you want to keep track of your objects or dynamically alter your objects
info[i].infowindow = infowindow;
infowindow.open(map,marker);
}
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width: 320px; height: 480px;">
</div>
</body>
</html>