美文网首页
加载地图

加载地图

作者: 特小懒虫 | 来源:发表于2020-04-15 15:11 被阅读0次

    创建的时候,加载js的文件

    export default function MP (key) {
      const JS1 = new Promise(function (resolve, reject) {
        window.onload = function () {
          resolve(window.AMap)
        }
        let script = document.createElement('script')
        script.type = 'text/javascript'
        script.src = 'https://webapi.amap.com/maps?v=1.4.6&key=' + key + '&callback=onload'
        script.onerror = reject
        document.head.appendChild(script)
      })
      const JS2 = new Promise(function (resolve, reject) {
        let script2 = document.createElement('script')
        script2.type = 'text/javascript'
        script2.src = 'https://webapi.amap.com/ui/1.0/main-async.js'
        script2.onerror = reject
        script2.onload = function () {
          resolve('success')
        }
        document.head.appendChild(script2)
      })
      return Promise.all([JS1, JS2]).then(function (result) {
        return result[0]
      }).catch(e => {
        console.log(e)
      })
    }
    
    

    使用vue, mounted加载,MP是引入上面的函数

    async mounted () {
        await MP ('这是你的地图key')
    },
    

    相关文章

      网友评论

          本文标题:加载地图

          本文链接:https://www.haomeiwen.com/subject/klpcvhtx.html