Maps 谷歌地图-信息窗口用计时器关闭

Maps 谷歌地图-信息窗口用计时器关闭,maps,openinfowindowhtml,Maps,Openinfowindowhtml,我对谷歌API地图有点小问题,我想知道是否有人能帮忙。我正试图让infowindows打开一些包含HREF的HTML。现在我可以使用“X”来关闭窗口,但是我更喜欢使用Javascript计时器的第二种方法来关闭信息窗口,如下所示 setTimeout('infowindow.close(map,marker)'2000') 但是,浏览器报告信息窗口在2秒过期后不存在。为了解决此问题,我尝试将infowindow对象和标记对象添加到全局范围内的数组中,但这并没有解决此问题 <!DOCTYPE

我对谷歌API地图有点小问题,我想知道是否有人能帮忙。我正试图让infowindows打开一些包含HREF的HTML。现在我可以使用“X”来关闭窗口,但是我更喜欢使用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> 
我已将代码恢复到原始状态,如果您能就此问题提供任何建议,我将不胜感激

<!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> 

var地理编码器;
var映射;
var标记=[];
var信息=[];
var i=0;
函数初始化()
{
geocoder=新的google.maps.geocoder();
var latlng=新的google.maps.latlng(40.768505,-111.853244);
变异性肌肽=
{
缩放:8,
中心:拉特林,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
map=new google.maps.map(document.getElementById(“map_canvas”),myOptions);
地址邮政编码('bb9 5pl','HTML');
地址邮政编码('bb9 7de','HTML');
地址邮政编码('bb12 7nq','HTML');
addPostCode('bb4 6be','HTML');
}
函数addPostCode(zip,html)
{
地理编码(
{
“地址”:zip
},功能(结果、状态)
{
if(status==google.maps.GeocoderStatus.OK)
{
map.setCenter(结果[0].geometry.location);
var marker=new google.maps.marker(
{
地图:地图,
位置:结果[0]。geometry.location,
姓名:zip
});
var infowindow=new google.maps.infowindow({
内容:html
});
google.maps.event.addListener(标记'mouseover',函数()
{ 
信息窗口。打开(地图、标记);
setTimeout('infowindow.close(map,marker)'2000');
});
i++;
}
其他的
{
警报(“地理编码因以下原因未成功:“+状态”);
}
});
}
函数checkZip(zip)
{
var距离=Number.MAX_值;
var指数=0;
地理编码(
{
“地址”:zip
},功能(结果、状态)
{
if(status==google.maps.GeocoderStatus.OK)
{
对于(ix=0;ix
infowindow
中的setTimeout调用在触发时超出范围。将该调用更改为以下内容以更正此问题: