Google地图无法每次都正确加载

时间:2013-12-13 16:32:32

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

有时,Google地图无法正确加载,也不允许插入标记:

enter image description here

这是我创建地图的代码:

var companyCreateMap;
var companyCreateMapInitialZoom = 7;                 

function companyLoadInitialMap()
{
  companyCreateGeocoder = new google.maps.Geocoder();

  var companyCreateInitialLocation = new google.maps.LatLng(companyCreateMapInitialCenterLat, companyCreateMapInitialCenterLng);

  var companyCreateMapOptions = {
    center: companyCreateInitialLocation,
    zoom: companyCreateMapInitialZoom,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}

companyCreateMap = new google.maps.Map(document.getElementById(companyCreateMapCanvas), companyCreateMapOptions);

google.maps.event.addListener(companyCreateMap, 'click', updateMarkerCoordenatesOnClick);

companyCreateGetUserLocation();
}

function companyCreateGetUserLocation()
{
  if (navigator.geolocation)
  {
    browserSupportFlag = true;

    navigator.geolocation.getCurrentPosition(function (companyCreatePosition)
    {
        var companyCreateCenterLocation = new google.maps.LatLng(companyCreatePosition.coords.latitude, companyCreatePosition.coords.longitude);

        companyCreateMap.setCenter(companyCreateCenterLocation);
        companyCreateMap.setZoom(17);
        companyCreateMarker = new google.maps.Marker({ position: companyCreateCenterLocation, map: companyCreateMap, animation: google.maps.Animation.DROP     });
    },
    function (error)
    {
        // User did not accept to give location
        //handleNoGeolocation(browserSupportFlag, error);
    });
}
    // Browser doesn't support Geolocation
else
{
    browserSupportFlag = false;
    //handleNoGeolocation(browserSupportFlag);
}
}

每次都不会发生这种情况,只是不时发生,我在控制台中没有出现任何错误。

有什么想法吗?

1 个答案:

答案 0 :(得分:6)

淡入完成后,您需要触发地图调整大小。

$('#map-canvas').fadeIn(200, function() {

    // Trigger a map resize
    google.maps.event.trigger(map, 'resize');
});

JSFiddle demo