location.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * 获取地理位置工具函数
  3. * @LOCATION_API_KEY 坐标点转位置信息的key
  4. * @getLocationDetail 坐标点转位置信息接口,此api基于腾讯地图
  5. * @getLocation 获取位置权限以及调用 getLocationInfo()
  6. * @getLocationInfo 根据权限获取位置信息
  7. * @getLocationAgain 权限被拒绝之后调用
  8. *
  9. * 使用方法:传入一个回调函数用以接收定位结果
  10. * getLocation((res)=>{})
  11. *
  12. * 该方法需要在小程序开发者中配置服务器域名
  13. * 参考:https://developers.weixin.qq.com/miniprogram/dev/framework/ability/network.html
  14. */
  15. const LOCATION_API_KEY = '5T3BZ-TDDL3-Q3K3G-36KRY-RSYH3-Y6B6R'
  16. // 根据经纬度获取地址详情接口
  17. const getLocationDetail = (latitude, longitude) => uni.$u.get(
  18. `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude.toString()},${longitude.toString()}&key=${LOCATION_API_KEY}`
  19. )
  20. // 获取地理位置
  21. export const getLocation = (callBack) => {
  22. uni.getSetting({
  23. success: async (res) => {
  24. if (res.authSetting['scope.userLocation']) {
  25. getLocationInfo(callBack)
  26. } else {
  27. uni.authorize({
  28. scope: 'scope.userLocation',
  29. success: () => {
  30. getLocationInfo(callBack)
  31. },
  32. fail: () => {
  33. uni.showToast({
  34. title: '位置权限获取失败',
  35. icon: 'none'
  36. });
  37. callBack({
  38. status: false,
  39. data: null
  40. })
  41. }
  42. })
  43. }
  44. }
  45. });
  46. }
  47. // 获取权限之后获取定位信息
  48. export const getLocationInfo = (callBack) => {
  49. uni.getLocation({
  50. type: ' wgs84',
  51. success: async (res) => {
  52. let e = await getLocationDetail(res.latitude, res
  53. .longitude)
  54. if (e.status == 0) {
  55. uni.setStorage({
  56. key: 'USER_LOCATION_INFO',
  57. data: e.result
  58. })
  59. callBack({
  60. status: true,
  61. data: e.result
  62. })
  63. } else {
  64. callBack({
  65. status: false,
  66. data: null
  67. })
  68. }
  69. }
  70. });
  71. }
  72. // 获取地理位置(被拒后再次)
  73. export const getLocationAgain = (callBack) => {
  74. uni.openSetting({
  75. success: (res) => {
  76. if (res.authSetting['scope.userLocation']) {
  77. // 授权成功
  78. uni.getLocation({
  79. type: 'wgs84',
  80. success: async (res) => {
  81. let e = await getLocationDetail(res.latitude,
  82. res.longitude)
  83. if (e.status == 0) {
  84. uni.setStorage({
  85. key: 'USER_LOCATION_INFO',
  86. data: e.result
  87. })
  88. callBack({
  89. status: true,
  90. data: e.result
  91. })
  92. } else {
  93. callBack({
  94. status: false,
  95. data: null
  96. })
  97. }
  98. },
  99. fail() {
  100. callBack({
  101. status: false,
  102. data: null
  103. })
  104. }
  105. });
  106. } else {
  107. callBack({
  108. status: false,
  109. data: null
  110. })
  111. }
  112. },
  113. fail() {
  114. callBack({
  115. status: false,
  116. data: null
  117. })
  118. }
  119. })
  120. }