map.js 2.18 KB
// 引入SDK核心类
// import QQMapWX from '../../../utils/qqmap-wx-jssdk.min.js'
const map_key = 'ZEJBZ-LVNRG-EXMQH-I74N6-EHNIS-WHF6E'
// 实例化API核心类
// const myMap = new QQMapWX({
//   key: map_key // 必填
// })

/**逆地址解析 */

const detailedAdrress = (tlng, tlat, da, fa, type) => {
  if (tlng && tlat) {
    let coord_type = 5;
    if (null != type) {
      coord_type = type
    }
    wx.showToast({
      title: tlng,
    })
    // myMap.reverseGeocoder({
    //   location: {
    //     latitude: tlat,
    //     longitude: tlng
    //   },
    //   coord_type: coord_type,
    //   success(res) {
    //     if (res.status == "0") {
    //       if (da && typeof da === 'function') {
    //         da(res.result)
    //       }
    //     }
    //   },
    //   fail(res) {
    //     if (fa && typeof fa === 'function') {
    //       fa(res)
    //     }
    //   }
    // })
  }
}
/**获取搜索周边信息 */
const searchAddress = (address,lat,lng,callBack,error) =>{
  if(!address) {
    myMap.reverseGeocoder({
      location: {
        latitude: lat,
        longitude: lng
      },
      success(res){
        search(res.result.address,lat,lng,callBack,error)
      }
    })
  }else{
    search(address,lat,lng,callBack,error)
  }
}
const search = (address,lat,lng,callBack,error) => {
  myMap.search({
    keyword: address,
    location: `${lat},${lng}`,
    success(res) {
      callBack(res)
    },
    fail(err) {
      error(err)
    }
  })
}
/**两点之间的距离 */
const calculateDistance = (flng, flat, tlng, tlat, cb) => {
  // console.log(flng + ","+ flat+","+ tlng+","+ tlat);
  //默认计算步行距离
  myMap.calculateDistance({
    from: {
      latitude: flat,
      longitude: flng
    },
    to: [{
      latitude: tlat,
      longitude: tlng
    }],
    success: function (res) {
      if (res.status == "0") {
        if (cb && typeof cb === 'function') {
          cb(res.result.elements[0].distance)
        }
      }
    },
    fail: function (res) {
      console.log(res)
    }
  })
}
module.exports = {
  detailedAdrress,
  calculateDistance,
  searchAddress
}