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



这是我创建地图的代码:

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);
}
}


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

任何想法?

最佳答案

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

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

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


JSFiddle demo

09-07 19:11