export default function () {
  loadMapScript.call(this);
}
function loadMapScript() {
  //基础地图底层
  const script = document.createElement("script");
  script.src =
    "https://webapi.amap.com/maps?v=1.4.15&key=9066916e054aa3083a6f081a2a9f9d7a&plugin=AMap.ControlBar,AMap.MarkerClusterer";
  document.body.appendChild(script);

  //loca图层
  const script2 = document.createElement("script");
  script2.src =
    "https://webapi.amap.com/loca?v=1.3.0&key=9066916e054aa3083a6f081a2a9f9d7a";
  document.body.appendChild(script2);

  //视频调度sdk
  const script4 = document.createElement("script");
  script4.src = "https://sqimg.qq.com/expert_qq/webrtc/3.0.6/WebRTCAPI.min.js";
  document.body.appendChild(script4);

  const script3 = document.createElement("script");
  script3.src = "http://cache.amap.com/lbs/static/addToolbar.js";
  if (!"AMap" in window) {
    document.body.appendChild(script3);
  }
  loading.call(this);
}
function loading ()  {
  let _this = this;
  function nextStep() {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve("AMap" in window);
      }, 500);
    });
  }

  async function ajaxMap() {
    let ifComplete = false;
    for (var i = 0; i < 10; i++) {
      if (!ifComplete) {
        console.log("没有");
        ifComplete = await nextStep();
      } else {
        if (ifComplete) {
          const { AMap, Loca } = window;
          console.log("第" + i + "次加载" + "地图初始化中。。。");
          if (!AMap) {
            loadMapScript.call(_this);
          } else {
           _this.initialMap();
          }
          break;
        } else {
          message.error("网络超时");
        }
      }
    }
  }
  ajaxMap();
};