I am trying to load the Bing Maps API during runtime of my JavaScript file. Here is what i have got so far, based on this.
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();
});
But i will get the following error
TypeError: Microsoft.Maps.NetworkCallbacks.f_logCallbackRequest is not a function
So I tried another attempt:
$('head').append('<script type="text/javascript" src="https://www.bing.com/api/maps/mapcontrol?callback=loadMapScenario" async defer></script>');
load_bing_map();
But I am again running in errors:
jQuery.Deferred exception: n is null k@https://www.bing.com/api/maps/mapcontrol?callback=loadMapScenario:12:7416
In both cases the load_bing_map()
function looks like this.
function load_bing_map() {
var map = new Microsoft.Maps.Map('#mymap', {
credentials: 'MyKey'
});
}
The code is executed after the $().ready
trigger is fired.
What am I missing?
Finally I do not need a solution like
<head>
<script type='text/javascript' src='https://www.bing.com/api/maps/mapcontrol?callback=loadMapScenario' async defer></script>
</heady>
I can only do it during runtime.