utils.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. export function initGeocoder() {
  2. return new Promise((resolve, reject) => {
  3. window.AMap.plugin('AMap.Geocoder', () => {
  4. try {
  5. const geocoder = new window.AMap.Geocoder({
  6. // city 指定进行编码查询的城市,支持传入城市名、adcode 和 citycode
  7. city: '全国',
  8. })
  9. resolve(geocoder)
  10. } catch (error) {
  11. reject(error)
  12. }
  13. })
  14. })
  15. }
  16. export function geolocation() {
  17. return new Promise((resolve, reject) => {
  18. window.AMap.plugin('AMap.Geolocation', () => {
  19. const geolocation = new window.AMap.Geolocation({
  20. // 是否使用高精度定位,默认:true
  21. enableHighAccuracy: true,
  22. // 设置定位超时时间,默认:无穷大
  23. timeout: 10000,
  24. // 定位按钮的停靠位置的偏移量
  25. offset: [10, 20],
  26. // 定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
  27. zoomToAccuracy: true,
  28. // 定位按钮的排放位置, RB表示右下
  29. position: 'RB',
  30. })
  31. geolocation.getCurrentPosition((status, result) => {
  32. if (status === 'complete') {
  33. resolve(result)
  34. } else {
  35. reject(result)
  36. }
  37. })
  38. })
  39. })
  40. }
  41. export default {}