需求:在内网环境下不使用百度API加载地图
解决:
1. 思路:新建script标签,利用他的src属性加载地图,如果加载成功触发onload事件,否则触发onerror事件
2. 实现:
```javascript
var mapLoaded=true;
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
script.src= 'https://api.map.baidu.com/api?v=2.0&ak=sCR3yMyHDKatEAF1opbKapOE5raaMegq';
head.appendChild(script);
script.onload = script.onreadystatechange = function() {
if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete" ) {
script.onload = script.onreadystatechange = null;
}
};
script.onerror = function() {
mapLoaded=false
};
```
网友评论