map-utils.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. export function geolocation() {
  2. return new Promise((resolve, reject) => {
  3. window.AMap.plugin('AMap.Geolocation', () => {
  4. const geolocation = new window.AMap.Geolocation({
  5. // 是否使用高精度定位,默认:true
  6. enableHighAccuracy: true,
  7. // 设置定位超时时间,默认:无穷大
  8. timeout: 10000,
  9. })
  10. geolocation.getCurrentPosition((status, result) => {
  11. if (status === 'complete') {
  12. resolve(result)
  13. } else {
  14. reject(result)
  15. }
  16. })
  17. })
  18. })
  19. }
  20. // 高德转百度坐标
  21. export function convertor(lng, lat) {
  22. return new Promise((resolve, reject) => {
  23. const convertor = new window.BMapGL.Convertor()
  24. // 高德转百度
  25. convertor.translate([new window.BMapGL.Point(lng, lat)], 3, 5, (data) => {
  26. if (data.status === 0) {
  27. resolve(data.points[0])
  28. } else {
  29. reject()
  30. }
  31. })
  32. })
  33. }
  34. // 地址导航
  35. export async function mapNavigate(options = {}, origin) {
  36. console.log(options)
  37. // 百度
  38. if (origin === 'baidu') {
  39. const point = await convertor(options.lng, options.lat)
  40. console.log(point)
  41. console.log('百度地图')
  42. options.locationUrl = `http://api.map.baidu.com/marker?location=${point.lat},${point.lng}&title=${options.title}&content=${options.address}&output=html&src=webapp.baidu.openAPIdemo`
  43. }
  44. // 腾讯
  45. if (origin === 'tx') {
  46. console.log('腾讯地图')
  47. options.locationUrl = `https://apis.map.qq.com/uri/v1/marker?marker=coord:${options.lat},${options.lng};${options.title};addr:${options.address}&referer=BWUBZ-LRLCQ-JON5T-GJLC4-URIMQ-CRBO6`
  48. }
  49. // 高德
  50. if (origin === 'gaode') {
  51. console.log('高德地图')
  52. options.locationUrl = `https://uri.amap.com/marker?position=${options.lng},${options.lat}&name=${options.title}&coordinate=gaode&callnative=0`
  53. }
  54. // return
  55. // window.open(options.locationUrl)
  56. window.location.href = options.locationUrl
  57. }