我想在JavaScript文件運行時加載Bing Maps API。這是我到目前爲止,根據this。在運行時加載BingMaps API
var bingmap_link = 'https://www.bing.com/api/maps/mapcontrol?callback=loadMapScenario';
$.when(
$.getScript(bingmap_link),
$.Deferred(function(deferred){
$(deferred.resolve);
})
)
.done(function() {
console.log("done");
load_bing_map();
});
,但我會收到以下錯誤
TypeError: Microsoft.Maps.NetworkCallbacks.f_logCallbackRequest is not a function
於是,我又一次嘗試:
$('head').append('<script type="text/javascript" src="https://www.bing.com/api/maps/mapcontrol?callback=loadMapScenario" async defer></script>');
load_bing_map();
但我在錯誤再次運行。
jQuery.Deferred exception: n is null [email protected]https://www.bing.com/api/maps/mapcontrol?callback=loadMapScenario:12:7416
在這兩種情況下的load_bing_map()
功能看起來像這樣。
function load_bing_map() {
var map = new Microsoft.Maps.Map('#mymap', {
credentials: 'MyKey'
});
}
代碼在$().ready
觸發器被觸發後執行。
我錯過了什麼?
最後我不需要像
<head>
<script type='text/javascript' src='https://www.bing.com/api/maps/mapcontrol?callback=loadMapScenario' async defer></script>
</heady>
我只能做到這一點wuring運行的解決方案。
非常感謝。
完美的作品。謝謝 :) – Torben