Google Maps API v3地图未完全加载

时间:2013-03-25 03:17:09

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

我在谷歌地图上遇到了一些困难。问题是只有一小部分地图正在加载,如下所示:

enter image description here

页面加载后,如果我尽量调整浏览器的大小,这会导致整个地图刷新并正确加载,如下所示: enter image description here

这是我的javascript代码:

google.maps.event.addDomListener(window, 'load', initialize);

$(document).ready(function(){                               
    var $width = document.getElementById("propertysection").offsetWidth;
    $('#map-canvas-2').width($width-28-175);
    $('#map-canvas-2').height($width);
    $('#myGridMap').height($width);
});

function initialize() { 
    var map; 
    var propertyMap;
    var marker; 
    var infowindow;
        var myOptions = {
            zoom: 6,
            center:new google.maps.LatLng(50.7,-86.05),
            mapTypeId: google.maps.MapTypeId.HYBRID
        }
        map = new google.maps.Map(document.getElementById("map-canvas-2"),myOptions);                  
        infowindow = new google.maps.InfoWindow({
            content: 'Property Info',
            position: new google.maps.LatLng(0, 0)
        }); 
    } 

有谁可以提出问题可能是什么?感谢。

2 个答案:

答案 0 :(得分:5)

您不应将jquery ready方法与窗口加载事件混合使用(请参阅http://api.jquery.com/ready/)。

相反,从.ready函数中调用initialize,或者将窗口调整大小函数放在initialize方法本身中(在初始化映射之前)。

答案 1 :(得分:0)

我正在使用bootstrap和一个大型菜单,我使用官方的 Google Maps API异步加载功能解决了这个问题。 https://developers.google.com/maps/documentation/javascript/tutorial#asynch

   function initialize() {

        var myLatLng = new google.maps.LatLng(37.4221147, -122.0867373);

        var map_canvas = document.getElementById('map_canvas');

        var map_options = {
            center: new google.maps.LatLng(37.4221147, -122.0867373),
            zoom: 12,
                    mapTypeControl: false,
                    panControl: false,
            zoomControlOptions: {
                        position: google.maps.ControlPosition.LEFT_CENTER
            },
            streetViewControl: false,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }

        var map = new google.maps.Map(map_canvas, map_options);

        var contentString =
            '<b>Google Gate Bridge</b>' +
            '<p>Mountain View, CA 94043, USA</p>';

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

        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
        });

        google.maps.event.addListener(marker, 'click', function(){
            infowindow.open(map,marker);
        });

    }

    function loadScript() {
      var script = document.createElement("script");
      script.type = "text/javascript";
      script.src = "https://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize";
      document.body.appendChild(script);
    }

    var tab = document.getElementById('YourHtmlObjectID');
    YourHtmlObjectID.onmouseover = loadScript;
    YourHtmlObjectID.onclick = loadScript;