map.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { getCurrentLocation } from '@/api/client/home';
  2. export function getMapLocation() {
  3. uni.chooseLocation({
  4. success: res => {
  5. console.log(res);
  6. // this.getRegionFn(res);
  7. },
  8. fail: () => {
  9. // 如果用uni.chooseLocation没有获取到地理位置,则需要获取当前的授权信息,判断是否有地理授权信息
  10. uni.getSetting({
  11. success: res => {
  12. console.log(res);
  13. var status = res.authSetting;
  14. if (!status['scope.userLocation']) {
  15. // 如果授权信息中没有地理位置的授权,则需要弹窗提示用户需要授权地理信息
  16. uni.showModal({
  17. title: '是否授权当前位置',
  18. content: '需要获取您的地理位置,请确认授权,否则地图功能将无法使用',
  19. success: tip => {
  20. if (tip.confirm) {
  21. // 如果用户同意授权地理信息,则打开授权设置页面,判断用户的操作
  22. uni.openSetting({
  23. success: data => {
  24. // 如果用户授权了地理信息在,则提示授权成功
  25. if (data.authSetting['scope.userLocation'] === true) {
  26. uni.showToast({
  27. title: '授权成功',
  28. icon: 'success',
  29. duration: 1000,
  30. });
  31. // 授权成功后,然后再次chooseLocation获取信息
  32. uni.chooseLocation({
  33. success: res => {
  34. console.log('详细地址', res);
  35. // this.getRegionFn(res);
  36. },
  37. });
  38. } else {
  39. uni.showToast({
  40. title: '授权失败',
  41. icon: 'none',
  42. duration: 1000,
  43. });
  44. }
  45. },
  46. });
  47. }
  48. },
  49. });
  50. }
  51. },
  52. fail: res => {
  53. uni.showToast({
  54. title: '调用授权窗口失败',
  55. icon: 'none',
  56. duration: 1000,
  57. });
  58. },
  59. });
  60. },
  61. });
  62. }