2011-07-20 43 views
1

有沒有人使用谷歌地圖API超時?我有一個從融合表中加載的谷歌地圖。如果地圖不能在x秒內加載,我的客戶希望我採取不同的解決方案。設置谷歌地圖超時限制,以及如何測試?

我試過使用JavaScript超時函數和$(document).ready,但發現jquery ready在映射甚至渲染到屏幕之前發射。

所有這些......是否有人知道強制超時測試一些可用解決方案的方法。

感謝, 安迪

回答

2

你能使用tilesloaded事件?代碼看起來像這樣:

// set up the map and timeout 
var map = new google.maps.Map(document.getElementById("map"), someOptions), 
    timeoutSeconds = 4, 
    usingMap = false; 

function doAlternateStuff() { 
    // do whatever the fallback requires 
} 

// set a timeout and store the reference 
var timer = window.setTimeout(doAlternateStuff, timeoutSeconds * 1000); 

// now listen for the tilesloaded event 
google.maps.event.addListener(map, 'tilesloaded', function() { 
    // cancel the timeout 
    window.clearTimeout(timer); 
    usingMap = true; 
    // etc 
}); 
+0

謝謝!那正是我所期待的。 – AndySousa

+0

謝謝!我正在和Angular 1合作,並且我使用你的答案: 'var timer = $ timeout(doAlternateStuff,timeoutSeconds * 1000); google.maps.event.addListener(map,'tilesloaded',function(){ $ timeout.cancel(timer); });'' –