1

我按照 Google API 教程创建了一些简单的地图。使用的代码如下所示,数组中只有两个标记,但使用了更多。由于地图上的一些标记靠近在一起,我想将标记读入选择/选项下拉菜单中,因此除了能够单击标记之外,还可以通过数组中的名称“洞穴:”来选择标记。我还没有找到一种方法来做到这一点。这可能吗?

还有一种方法可以在打开新信息窗口时关闭以前的信息窗口吗?

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?&amp;sensor=false"></script>
<script type="text/javascript">
      function initialize() {
        var mapOptions = {
          mapTypeId: google.maps.MapTypeId.SATELLITE
        };
        var map = new google.maps.Map(document.getElementById("map-canvas"),
        mapOptions);

    var markers = [
    { lat: 51.589994, lng: -4.112497, cave: "Cat Hole Cave", name: "<h2>Cat Hole Cave</h2><img src='http://www.ogof.org.uk/Pictures/Entrances/1118.jpg' width='200' height='150'><br><h3><A href='http://www.cambriancavingcouncil.org.uk/registry/ccr_registry_view.php?ID=1118' target=_blank>Cambrian Cave Registry Entry</A><br><br>SS 53757 90020<br>Altitude 51m<br>Length 30m</h3>", site: "blue_cave.png"},
    { lat: 51.564564, lng: -4.274611, cave: "Mewslade Quarry Cave - Left", name: "<h2>Mewslade Quarry Cave - Left Entrance</h2><img src='http://www.ogof.org.uk/Pictures/Entrances/1570.jpg' width='200' height='150'><br><h3><A href='http://www.cambriancavingcouncil.org.uk/registry/ccr_registry_view.php?ID=1570' target=_blank>Cambrian Cave Registry Entry</A><br><br>SS 42440 87530<br>Altitude TBCm<br>Length 10m<br><br>Photo courtesy of Gary Evans</h3>", site: "blue_cave.png" },
];

for (index in markers) addMarker(markers[index]);
function addMarker(data) {

    var marker = new google.maps.Marker({
    position: new google.maps.LatLng(data.lat, data.lng),
    map: map,
    title: data.cave,
    icon: data.site
  });     
  var content = document.createElement("DIV");
  var title = document.createElement("DIV");
  title.innerHTML = data.name;
  content.appendChild(title);
  var infowindow = new google.maps.InfoWindow({
  content: content,
  maxWidth: 180
    });

  google.maps.event.addListener(marker, "click", function() {
  infowindow.open(map, marker);
  });
}
var bounds = new google.maps.LatLngBounds();
    for (index in markers) {
      var data = markers[index];
      bounds.extend(new google.maps.LatLng(data.lat, data.lng));
    }
    map.fitBounds(bounds);
  }   
</script>
</head>
<body onload="initialize()">
<div id="map-canvas"></div>
</body>

这是该页面的链接,因为它目前有效。 http://www.ogof.org.uk/Gower-Cave-Location-Map.html

4

0 回答 0