2010-08-17 69 views
0

即時通訊谷歌API地圖有一點問題,我想知道是否有人可以提供幫助。我試圖讓infowindows在包含HREF的HTML中打開。現在我可以使用「X」關閉窗口,但是id更喜歡使用Javascript定時器的第二種方法來關閉信息窗口,如下所示。谷歌地圖 - 信息窗口關閉與計時器

setTimeout('infowindow.close(map,marker)','' 2000' );

然而,瀏覽器報告在2秒過期後信息窗口不存在。爲了解決這個問題,我試圖將infowindow對象和標記對象添加到全局範圍的數組中,但是這並沒有解決問題。

我已經將代碼返回到原始狀態,將不勝感激任何建議在這個問題上..

<!DOCTYPE html> 
    <html> 
    <head> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
    <title></title> 
    <link href="http://code.google.com/apis/maps/documentation/javascript/examples/standard.css" rel="stylesheet" type="text/css" /> 
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
    <script type="text/javascript"> 

    var geocoder; 
    var map; 
    var markers = []; 
    var info = []; 
    var i = 0; 

    function initialize() 
    { 
     geocoder = new google.maps.Geocoder(); 
     var latlng = new google.maps.LatLng(40.768505, -111.853244); 
     var myOptions = 
     { 
      zoom: 8, 
      center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     } 
     map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
     addPostCode('bb9 5pl','HTML'); 
     addPostCode('bb9 7de','HTML'); 
     addPostCode('bb12 7nq','HTML'); 
     addPostCode('bb4 6be','HTML'); 
    } 

    function addPostCode(zip, html) 
    { 
     geocoder.geocode(
     { 
      'address': zip 
     }, function (results, status) 
     { 
      if (status == google.maps.GeocoderStatus.OK) 
      { 
      map.setCenter(results[0].geometry.location); 
      var marker = new google.maps.Marker(
      { 
       map: map, 
       position: results[0].geometry.location, 
       name: zip 
      }); 

      var infowindow = new google.maps.InfoWindow({ 
      content: html 
      }); 

      google.maps.event.addListener(marker, 'mouseover', function() 
      { 
       infowindow.open(map,marker); 
       setTimeout('infowindow.close(map,marker)','2000'); 
      }); 


      i++; 
      } 
      else 
      { 
      alert("Geocode was not successful for the following reason: " + status); 
      } 
     }); 
    } 

    function checkZip(zip) 
    { 
     var distance = Number.MAX_VALUE; 
     var index = 0; 
     geocoder.geocode(
     { 
      'address': zip 
     }, function (results, status) 
     { 
      if (status == google.maps.GeocoderStatus.OK) 
      { 
      for (ix = 0; ix < markers.length; ix++) 
      { 
       var tmp = getDistance(results[0].geometry.location, markers[ix].position); 
       if (tmp < distance) 
       { 
        distance = tmp; 
        index = ix; 
       } 
      } 
      alert('nearest zipcode is :' + markers[index].name + ' Distance:' + distance) 
      } 
     }); 
    } 

    function getDistance(latlng1, latlng2) 
    { 
     var R = 6371; // Radius of the earth in km 
     var dLat = (latlng2.lat() - latlng1.lat()) * Math.PI/180; // Javascript functions in radians 
     var dLon = (latlng2.lng() - latlng1.lng()) * Math.PI/180; 
     var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(latlng1.lat() * Math.PI/180) 
       * Math.cos(latlng2.lat() * Math.PI/180) * Math.sin(dLon/2) * Math.sin(dLon/2); 
     var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); 
     var d = R * c; // Distance in km 
     d = d * 0.621371192; 
     return d; 
    } 

    </script> 
    </head> 
    <body onload="initialize()"> 
     <div> 
     <input id="address" type="textbox" value=""> 
     <input type="button" value="Geocode" onclick="checkZip(getElementById('address').value)"> 
     </div> 
    <div id="map_canvas" style="height:500px; width: 600px;"></div> 
    </body> 
    </html> 

回答

1

在setTimeout調用infowindow超出範圍時觸發。將該呼叫更改爲以下內容以更正此問題使用Closures

setTimeout(function(){infowindow.close();}, '2000'); 
相關問題