好的,我已经找到了解决方案,我相信有人会说这是一个重复的话题,但我很困惑,为什么这是行不通的。bing map v7到v8迁移 - 脚本导入问题
这是我的代码的一个基本的例子,现在
main.html中
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<div id="myMap" style="position:relative;width:600px;height:400px;"></div>
</body>
<script>
var url =
//'http://www.bing.com/api/maps/mapcontrol?branch=experimental&callback=GetMap';
'https://www.bing.com/mapspreview/sdk/mapcontrol';
loadScript(url, test_function);
function test_function() {
console.log('inside test_function before map');
map = new Microsoft.Maps.Map('#myMap', {
credentials: 'My Bing Maps Key'});
console.log('inside test_function after map');
};
</script>
</html>
main.js
function loadScript(url, callback){
var script = document.createElement("script")
script.type = "text/javascript";
script.async = true;
script.defer = true;
if (script.readyState){ //IE
console.log('inside IE block');
script.onreadystatechange = function(){
if (script.readyState == "loaded" ||
script.readyState == "complete"){
script.onreadystatechange = null;
callback();
}
};
} else { //Others
console.log('inside Other block');
script.onload = function(){
callback();
};
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
}
控制台输出
main.js:18 inside Other block
main.html:17 inside test_function before map
mapcontrol:11 Uncaught TypeError: Cannot read property 'prototype' of null
at k (mapcontrol:11)
at n.h [as create] (mapcontrol:11)
at e (mapcontrol:11)
at t.l [as instance] (mapcontrol:11)
at n.h [as create] (mapcontrol:11)
at e (mapcontrol:11)
at t.l [as instance] (mapcontrol:11)
at new Microsoft.Maps.Map (mapcontrol:13)
at test_function (main.html:18)
at HTMLScriptElement.script.onload (main.js:20)
我可以得到一个基本的示例工作,但是当我尝试执行这种创建脚本元素并追加它的方法时,我什么也得不到。由于这是它在更大的应用程序中实现的方式,因此它来自v7,我不想重新构建它。
我尝试过像jQuery getScript之类的东西,而不是什么。
请让我知道你的想法!谢谢。