Google地图API V3在从链接打开时关闭infoWindow

时间:2012-08-24 11:31:10

标签: api google-maps google-maps-api-3

我正在使用多个标记和信息窗口在谷歌地图上从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>

1 个答案:

答案 0 :(得分: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())
}