map-utils.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. function mapInit(callback) {
  2. function loadScript() {
  3. var script = document.createElement('script')
  4. script.src =
  5. 'https://api.map.baidu.com/api?v=1.0&type=webgl&ak=vsIfSztpPmCtmBRfRiIAM57hbxBQbmgQ&callback=initMapApi'
  6. document.body.appendChild(script)
  7. }
  8. if (!window.BMapGL) {
  9. window.initMapApi = callback
  10. loadScript()
  11. } else {
  12. callback()
  13. }
  14. }
  15. // 定位当前位置
  16. export function loactionSelf() {
  17. return new Promise((resolve, reject) => {
  18. mapInit(() => {
  19. const BMapGL = window.BMapGL
  20. const geolocation = new BMapGL.Geolocation({
  21. // 是否使用高精度定位,默认:true
  22. enableHighAccuracy: true,
  23. // 设置定位超时时间,默认:无穷大
  24. timeout: 10000,
  25. })
  26. // 开启SDK辅助定位
  27. geolocation.enableSDKLocation()
  28. geolocation.getCurrentPosition(function (r) {
  29. // alert(JSON.stringify(r))
  30. console.log(r)
  31. // console.log(this)
  32. if (this.getStatus() === 0) {
  33. // alert(JSON.stringify(r))
  34. resolve({
  35. point: r.point,
  36. address: r.address,
  37. })
  38. } else {
  39. reject('failed' + this.getStatus())
  40. }
  41. })
  42. })
  43. })
  44. }
  45. export function geolocation() {
  46. return new Promise((resolve, reject) => {
  47. window.AMap.plugin('AMap.Geolocation', () => {
  48. const geolocation = new window.AMap.Geolocation({
  49. // 是否使用高精度定位,默认:true
  50. enableHighAccuracy: true,
  51. // 设置定位超时时间,默认:无穷大
  52. timeout: 10000
  53. })
  54. geolocation.getCurrentPosition((status, result) => {
  55. if (status === 'complete') {
  56. resolve(result)
  57. } else {
  58. reject(result)
  59. }
  60. })
  61. })
  62. })
  63. }
  64. // 地址导航
  65. export function mapNavigate(options = {}, origin) {
  66. console.log(options)
  67. // 百度
  68. if (origin === 'baidu') {
  69. console.log('百度地图')
  70. options.locationUrl = `http://api.map.baidu.com/marker?location=${options.lat},${options.lng}&title=${options.title}&content=${options.address}&output=html&src=webapp.baidu.openAPIdemo`
  71. }
  72. // 腾讯
  73. if (origin === 'tx') {
  74. console.log('腾讯地图')
  75. 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`
  76. }
  77. // 高德
  78. if (origin === 'gaode') {
  79. console.log('高德地图')
  80. options.locationUrl = `https://uri.amap.com/marker?position=${options.lng},${options.lat}&name=${options.title}&coordinate=gaode&callnative=0`
  81. }
  82. // window.open(options.locationUrl)
  83. window.location.href = options.locationUrl
  84. }