map-utils.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. var 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. if (this.getStatus() === 0) {
  31. // alert(JSON.stringify(r))
  32. resolve({
  33. point: r.point,
  34. address: r.address,
  35. })
  36. } else {
  37. reject('failed' + this.getStatus())
  38. }
  39. })
  40. })
  41. })
  42. }
  43. // 地址导航
  44. export function mapNavigate(options = {}, origin) {
  45. console.log(options)
  46. // 百度
  47. if (origin === 'baidu') {
  48. console.log('百度地图')
  49. 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`
  50. }
  51. // 腾讯
  52. if (origin === 'tx') {
  53. console.log('腾讯地图')
  54. 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`
  55. }
  56. // 高德
  57. if (origin === 'gaode') {
  58. console.log('高德地图')
  59. options.locationUrl = `https://uri.amap.com/marker?position=${options.lng},${options.lat}&name=${options.title}&coordinate=gaode&callnative=0`
  60. }
  61. // window.open(options.locationUrl)
  62. window.location.href = options.locationUrl
  63. }