艺易修小程序24.08.21
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
3.8 KiB

6 months ago
  1. import config from '../config.js'
  2. function calculateDistance(lat1, lon1, lat2, lon2) { //计算两点距离
  3. let distance = 0
  4. if (!lat2 || !lon2) return distance
  5. //先强制转换一下(后端给的字符串)
  6. lat1 = parseFloat(lat1)
  7. lon1 = parseFloat(lon1)
  8. lat2 = parseFloat(lat2)
  9. lon2 = parseFloat(lon2)
  10. // 将角度转换为弧度
  11. const R = 6371; // 地球半径,单位公里
  12. const dLat = (lat2 - lat1) * Math.PI / 180;
  13. const dLon = (lon2 - lon1) * Math.PI / 180;
  14. lat1 = lat1 * Math.PI / 180;
  15. lat2 = lat2 * Math.PI / 180;
  16. // Haversine公式
  17. const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
  18. Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
  19. const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  20. // 计算距离
  21. distance = R * c;
  22. return distance.toFixed(0)
  23. }
  24. function getLocation(fn) { //获取用户经纬度
  25. wxGetLocation() //此方法只用于提示用户打开gps
  26. uni.getLocation({
  27. type: 'gcj02',
  28. isHighAccuracy: true,
  29. highAccuracyExpireTime: 1000,
  30. success: function(position) {
  31. fn(position)
  32. },
  33. fail: function() { //使用ip获取定位
  34. let key = config.mapKey; //腾讯地图key
  35. getUserAddressByIp(key).then(res => {
  36. fn(res.position) //返回经纬度
  37. })
  38. }
  39. })
  40. }
  41. function getLocationDetail() { //获取用户详细地址
  42. wxGetLocation()
  43. return new Promise((resolve, reject) => {
  44. let key = config.mapKey; //腾讯地图key
  45. uni.getLocation({
  46. type: 'gcj02',
  47. isHighAccuracy: true,
  48. highAccuracyExpireTime: 1000,
  49. success: function(position) {
  50. getUserAddress(position.latitude, position.longitude, key).then(res => {
  51. resolve(res)
  52. })
  53. },
  54. fail: function() { //使用ip获取定位
  55. getUserAddressByIp(key).then(res => {
  56. resolve(res)
  57. })
  58. }
  59. })
  60. })
  61. }
  62. function getUserAddress(latitude, longitude, key) { //通过经纬度获取用户详细地址
  63. return new Promise((resolve, reject) => {
  64. uni.request({
  65. url: `/ws/geocoder/v1/?location=${latitude},${longitude}&key=${key}`,
  66. success: (res) => {
  67. let {
  68. lat,
  69. lng
  70. } = res.data.result.ad_info.location;
  71. let data = {
  72. position: {
  73. latitude: lat,
  74. longitude: lng
  75. },
  76. addressDetail: res.data.result.ad_info
  77. }
  78. resolve(data)
  79. },
  80. fail(err) {
  81. reject(err)
  82. }
  83. })
  84. })
  85. }
  86. function getUserAddressByIp(key) { //根据IP获取当前用户位置
  87. return new Promise((resolve, reject) => {
  88. uni.request({
  89. url: 'https://api.ipify.org?format=json',
  90. success: (ipInfo) => {
  91. uni.request({
  92. url: `/ws/location/v1/ip?ip=${ipInfo.data.ip}&key=${key}`,
  93. success: (res) => {
  94. let {
  95. lat,
  96. lng
  97. } = res.data.result.location;
  98. let data = {
  99. addressDetail: res.data.result.ad_info,
  100. ip: res.data.result.ip,
  101. position: {
  102. latitude: lat,
  103. longitude: lng
  104. }
  105. }
  106. resolve(data)
  107. },
  108. fail(err) {
  109. reject(err)
  110. }
  111. })
  112. }
  113. })
  114. })
  115. }
  116. //打开地图让用户选择位置
  117. function selectAddress(longitude, latitude, successCallback) {
  118. uni.chooseLocation({
  119. longitude, //经度
  120. latitude, //纬度
  121. success: function(res) {
  122. successCallback && successCallback(res)
  123. }
  124. });
  125. }
  126. //sdk自带获取位置方法(只支持微信环境),这里只当提示在用了(具体获取地址逻辑上面几个方法已完成)
  127. function wxGetLocation(successCallback, failCallback) {
  128. console.log('wx.getLocation');
  129. wx.getLocation({
  130. type: 'gcj02',
  131. isHighAccuracy: true,
  132. highAccuracyExpireTime: 2000,
  133. success(res) {},
  134. fail(err) {
  135. if(err.errMsg == 'getLocation:gps closed'){
  136. uni.showToast({
  137. title: '请打开GPS定位,否则定位不准确',
  138. icon: 'none'
  139. })
  140. }
  141. }
  142. })
  143. }
  144. export default {
  145. calculateDistance, //计算两点距离
  146. getLocationDetail, //获取用户详细地址
  147. getLocation, //获取用户经纬度
  148. selectAddress, //打开地图让用户选择位置
  149. wxGetLocation,
  150. }