0

我正在使用多个标记和信息窗口在谷歌地图上从 V2 迁移到 V3,并且地图工作正常,当单击标记时,信息窗口打开和关闭正常。

在地图旁边,我正在以常规文本链接格式列出所有点(标记),例如当用户单击“电影院”时,相应的信息窗口就会打开。我有这个工作但有问题。首先,地图本身不会移动以打开信息窗口,然后可以将其隐藏一半,当单击以关闭信息窗口时,它不会移动。在版本 2 中,我使用 Loughcrew 来执行此操作。这是我目前拥有的脚本:

<div id="map" style="width: 500px; height: 500px; border:solid 1px black; "></div>

<script type="text/javascript">
   var myLatlng = new Array();
   var  marker = new Array();
   var infowindow = new Array();                            

   function initialize() {
      //set the map options
      var mapOptions = {
          zoom: 8,
          center: new google.maps.LatLng(53.50046357326504,-6.8280029296875),
          streetViewControl: true,
          overviewMapControl:true,
          mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      var map = new google.maps.Map(document.getElementById("map"), mapOptions);
      myLatlng[0] = new google.maps.LatLng(53.738315087044704,-7.80029296875);
      //marker
      marker[0] = new google.maps.Marker({
         position: myLatlng[0],
         map: map,
         icon: 'bus.png',
         title:"This is test 1"
      });
      infowindow[0] = new google.maps.InfoWindow({
         content: 'Hey, here is come info'
      });

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

      myLatlng[1] = new google.maps.LatLng(53.12633883947352,-7.3443603515625);
      //marker
      marker[1] = new google.maps.Marker({
         position: myLatlng[1],
         map: map,
         icon: 'tennis-sports.png',
         title:"This is another test again"
      });

      infowindow[1] = new google.maps.InfoWindow({
         content: 'And more info here!'
      });

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

/********************* end of markers  ********************/
   }//end initialize

   function loadScript() {
      var script = document.createElement("script");
      script.type = "text/javascript";
      script.src = "http://maps.googleapis.com/maps/api/js?key=xxxxxxxx&sensor=false&callback=initialize";
      document.body.appendChild(script);
   }

   window.onload = loadScript;

</script>

<a href="javascript:infowindow[1].open(map, marker[1]);" id="link1">Test link</a>
4

1 回答 1

1

试试这样:

<a href="javascript:openInfoWindow(1);" id="link1">Test link</a>

function openInfoWindow(i) {
    infowindow[i].open(map, marker[i])
    map.setCenter(marker[i].getPosition())
}
于 2012-08-24T12:04:23.427 回答