2012-07-12 126 views
0

我使用具有Map的加載方法顯示頁面。 問題是頁面被加載,但在地圖div顯示灰色的屏幕。 當我刷新頁面地圖加載。我也調用文檔就緒功能的地圖功能。但沒用。顯示無刷新頁面的地圖。

我的代碼是

<div class="public-view" id="map_canvass"> 
<script type="text/javascript"> 
// OnLoad function ... 
initialize(); 
function initialize() { 
    var fenway = new google.maps.LatLng(<?php echo $propertyValues->lat; ?>, <?php echo $propertyValues->lng; ?>); 
    var mapOptions = { 
     center: fenway, 
     zoom: 15, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map = new google.maps.Map(document.getElementById("map_canvass"), mapOptions); 
    var circleOptions = { 
     center: fenway, 
     map: map, 
     strokeColor: "#BB0A68", 
     strokeOpacity: 0.1, 
     fillColor: "#BB0A68", 
     fillOpacity: 0.35, 
     radius: 200 
    } 
    var circle = new google.maps.Circle(circleOptions); 
    google.maps.event.trigger(map, 'resize'); 
} 
</script> 
</div> 

回答

1

你需要調用初始化(),您知道 google.maps後,已加載。上面的代碼可能會在google.maps加載之前調用initialize。它第二次刷新頁面,因爲它已被緩存。 jQuery的文檔準備功能也不是100%可靠的。

This example從谷歌地圖API文檔顯示你做的最好的方式:

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