簡體   English   中英

Google Map Ajax加載

[英]Google Map Ajax load

我在Google地圖上獲得了“樂趣”,並且需要一些幫助。 基本上,我有一小段HTML / Javascript,可以使用Ajax將其加載到標准HTML頁面或div中。 我目前正在嘗試做的就是讓地圖出現。 當頁面作為HTML的一部分加載時,可以正常顯示,唱歌,跳舞和偶爾吹奏長笛。 但是,當它通過Ajax加載時,它不會出現,但是Google編寫的JavaScript會出現。

我想我缺少負載或就緒觸發器。 但找不到我想念的東西。 我的搜索-foo很弱。 這是代碼,您不會看到那么復雜。 谷歌地圖包括已被排除在外,但沒有錯誤或警告,沒有報告,正如我所說的,它可以作為HTML頁面正常工作。

<div id="map_canvas_<?= $userContact->getId() ?>" style="margin: 10px auto; width: 90%; height: 300px;"></div>
<script type="text/javascript">

    var mapInstance = null;

    var startLatitude = 53.975044;
    var startLongitude = -2.153320;
    var startZoomLevel = 5;

// create the new instance.
    mapInstance = new GMap2($('map_canvas_<?= $userContact->getId() ?>'));

    // center the map on the UK
    mapInstance.setCenter(new GLatLng(startLatitude, startLongitude), startZoomLevel);

</script>

卡恩。

您是否嘗試過將AJAX調用的結果傳遞給javascript的eval()方法? 這將執行從另一頁面加載的任何javascript代碼。

如果您只是使用AJAX加載js並將其添加到div中,那么代碼將不會自動執行。

我認為您需要在window.onload函數中創建GMap2實例等。 例如:

// ..other code stays the same
function initMap() {
    if (GBrowserIsCompatible()) { 
        mapInstance = new GMap2(..);
        mapInstance.setCenter(..);
    }
}
window.onload = initMap;

希望能解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM