【问题标题】:Google map API V3 issue closing the infoWindow when opened from a link从链接打开时关闭 infoWindow 的 Google map API V3 问题
【发布时间】:2012-08-24 11:31:10
【问题描述】:

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

【问题讨论】:

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


    【解决方案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())
    }
    

    【讨论】:

    • 谢谢。我试过你的建议,问题还是一样。
    • 那么发生了什么? Firebug 告诉你什么?可以发网址吗?
    • 将 init() 外部的 var map 定义为全局变量。 function openInfoWindow() 'can't see' 在其他函数中定义的变量。
    • 谢谢 Odoakr,这就是问题所在。也感谢 user1620090
    • 没问题 - 如果你喜欢我的回答,如果使用绿色勾号,请接受:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 2011-11-06
    • 2012-02-18
    • 2012-09-24
    相关资源
    最近更新 更多