Javascript 谷歌地图Api V3加载多页面刷新

Javascript 谷歌地图Api V3加载多页面刷新,javascript,google-maps-api-3,Javascript,Google Maps Api 3,我有这个脚本来初始化谷歌地图。初始化函数初始化地图。问题是它可以工作,但不是第一次当页面打开时。我必须刷新它两到三次,地图才会显示。为什么会这样 正在对body OnLoad事件调用initialize函数 而且,在两到三次页面刷新后,它不会加载到除chrome之外的任何其他浏览器中 var infowindow = new google.maps.InfoWindow(); var places=[]; //contains the addresses of marker

我有这个脚本来初始化谷歌地图。初始化函数初始化地图。问题是它可以工作,但不是第一次当页面打开时。我必须刷新它两到三次,地图才会显示。为什么会这样

正在对body OnLoad事件调用initialize函数

而且,在两到三次页面刷新后,它不会加载到除chrome之外的任何其他浏览器中

  var infowindow = new google.maps.InfoWindow();
  var places=[];        //contains the addresses of markers

//Find User GeoLocation to Show On Map for the First TIme Map opens.
    if (navigator.geolocation)
    {
            navigator.geolocation.getCurrentPosition(showPosition)                                                             
    }
    else
    {
            alert("Geolocation is not supported by this browser.");
    }

    function showPosition(position)
    {
      latitude=position.coords.latitude;
      longitude= position.coords.longitude;
    }


//Initialize Google Map Api
function initialize()
{
    geocoder = new google.maps.Geocoder();

    //Initial Map Variables
    var myoptions={
            zoom:8,
            center:new google.maps.LatLng(latitude,longitude),
            mapTypeId:google.maps.MapTypeId.ROADMAP
        };  

        //Initialize Map
        map=new google.maps.Map(document.getElementById("map_canvas"),myoptions);           

    });

}

我说这是因为地理定位返回时未知。它是异步的。如果地图在地理定位完成之前尝试加载,则不会设置变量纬度和经度,地图也不会加载

确保先进行地理定位,然后就可以了

HTML:


thnx它在chrome中工作,但在其他浏览器中仍然没有显示为什么???我在Firefox 12.0上试过,Opera没有工作,我没有最近的IE。你有geoloc错误吗?我正试图找出如何从错误中提取更多信息。我会用我发现的任何东西更新。我等了一会儿,地图出现了。它在opera中也起了作用。太棒了!否则我很难找出问题所在。我的歌剧院抱怨没有位置。我建议在页面中添加旋转加载gif,并在加载Google地图时将其删除。如果出现错误,使用预设的lat、lng调用初始化功能
function getLocation() {
var infowindow = new google.maps.InfoWindow();
  var places=[];        //contains the addresses of markers

//Find User GeoLocation to Show On Map for the First TIme Map opens.
    if (navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(showPosition, function(err) { alert(err + " " + err.code);});
    }
    else
    {
            alert("Geolocation is not supported by this browser.");
    }
}

    function showPosition(position)
    {
      latitude=position.coords.latitude;
      longitude= position.coords.longitude;
      initialize(latitude, longitude);
    }

//Initialize Google Map Api
function initialize(latitude, longitude)
{
    geocoder = new google.maps.Geocoder();

    //Initial Map Variables
    var myoptions={
            zoom:8,
            center:new google.maps.LatLng(latitude,longitude),
            mapTypeId:google.maps.MapTypeId.ROADMAP
        };  

        //Initialize Map
        map=new google.maps.Map(document.getElementById("map_canvas"),myoptions);           

}
<body onload="getLocation()">

<div id="map_canvas"></div>

</body>