百度地图.md 1.4 KB

 function LoadBaiduMapScript () {
   const src = 'https://api.map.baidu.com/api?v=2.0&ak=mt5wC6QOL4EpCpwqx2EiiPbOql1cxwRI&callback=onBMapCallback'
   return new Promise((resolve, reject) => {
     if (typeof BMap !== 'undefined') {
       resolve(BMap)
       return
     }
     window.onBMapCallback = function () {
       console.log('BMap is loaded')
       resolve(BMap)
     }
     const scriptNode = document.createElement('script')
     scriptNode.src = src
     scriptNode.type = 'text/javascript'
     scriptNode.onerror = reject
     scriptNode.onload = () => {
       console.log('BMap scriptNode is loaded')
     }

     document.body.appendChild(scriptNode)
   })
 }

 getLocationByBaidu () {
       LoadBaiduMapScript().then(BMap => {
         const geolocation = new BMap.Geolocation()
         var _this = this
         geolocation.getCurrentPosition(
           async function getinfo (position) {
             console.log(JSON.stringify(position.address))
             let latitude = position.latitude // 纬度
             let longitude = position.longitude // 经度
             _this.locationOrgName(latitude, longitude)
           },
           function (e) {
             _this.$vux.toast.text('定位失败', 'middle')
           },
           { provider: 'baidu' }
         )
       })
         .catch(err => {
           console.error(err)
           _this.$vux.toast.text('加载地图定位失败', 'middle')
         })
     },