map-utils.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. // 地址导航
  46. export function mapNavigate(options = {}, origin) {
  47. console.log(options)
  48. // 百度
  49. if (origin === 'baidu') {
  50. console.log('百度地图')
  51. 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`
  52. }
  53. // 腾讯
  54. if (origin === 'tx') {
  55. console.log('腾讯地图')
  56. 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`
  57. }
  58. // 高德
  59. if (origin === 'gaode') {
  60. console.log('高德地图')
  61. options.locationUrl = `https://uri.amap.com/marker?position=${options.lng},${options.lat}&name=${options.title}&coordinate=gaode&callnative=0`
  62. }
  63. // window.open(options.locationUrl)
  64. window.location.href = options.locationUrl
  65. }