瑶都万能墙
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.

189 lines
4.4 KiB

4 months ago
3 months ago
4 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. uni.getFuzzyLocation({
  41. type: 'gcj02',
  42. // type: 'wgs84',
  43. success (res) {
  44. fn(res)
  45. }
  46. })
  47. }
  48. function getLocationDetail() { //获取用户详细地址
  49. wxGetLocation()
  50. return new Promise((resolve, reject) => {
  51. let key = config.mapKey; //腾讯地图key
  52. // uni.getLocation({
  53. // type: 'gcj02',
  54. // isHighAccuracy: true,
  55. // highAccuracyExpireTime: 1000,
  56. // success: function(position) {
  57. // getUserAddress(position.latitude, position.longitude, key).then(res => {
  58. // resolve(res)
  59. // })
  60. // },
  61. // fail: function() { //使用ip获取定位
  62. // getUserAddressByIp(key).then(res => {
  63. // resolve(res)
  64. // })
  65. // }
  66. // })
  67. getLocation((position) => {
  68. getUserAddress(position.latitude, position.longitude, key).then(res => {
  69. resolve(res)
  70. })
  71. })
  72. })
  73. }
  74. function getUserAddress(latitude, longitude, key) { //通过经纬度获取用户详细地址
  75. return new Promise((resolve, reject) => {
  76. let url = `/ws/geocoder/v1/?location=${latitude},${longitude}&key=${key}`
  77. // #ifndef H5
  78. url = `https://apis.map.qq.com` + url
  79. // #endif
  80. uni.request({
  81. url,
  82. success: (res) => {
  83. let {
  84. lat,
  85. lng
  86. } = res.data.result.ad_info.location;
  87. let data = {
  88. position: {
  89. latitude: lat,
  90. longitude: lng
  91. },
  92. addressDetail: res.data.result.ad_info
  93. }
  94. resolve(data)
  95. },
  96. fail(err) {
  97. reject(err)
  98. }
  99. })
  100. })
  101. }
  102. function getUserAddressByIp(key) { //根据IP获取当前用户位置
  103. return new Promise((resolve, reject) => {
  104. uni.request({
  105. url: 'https://api.ipify.org?format=json',
  106. success: (ipInfo) => {
  107. let url = `/ws/location/v1/ip?ip=${ipInfo.data.ip}&key=${key}`
  108. // #ifndef H5
  109. url = `https://apis.map.qq.com` + url
  110. // #endif
  111. uni.request({
  112. url,
  113. success: (res) => {
  114. let {
  115. lat,
  116. lng
  117. } = res.data.result.location;
  118. let data = {
  119. addressDetail: res.data.result.ad_info,
  120. ip: res.data.result.ip,
  121. position: {
  122. latitude: lat,
  123. longitude: lng
  124. }
  125. }
  126. resolve(data)
  127. },
  128. fail(err) {
  129. reject(err)
  130. }
  131. })
  132. }
  133. })
  134. })
  135. }
  136. //打开地图让用户选择位置
  137. function selectAddress(longitude, latitude, successCallback) {
  138. uni.chooseLocation({
  139. // longitude, //经度
  140. // latitude, //纬度
  141. success: function(res) {
  142. successCallback && successCallback(res)
  143. }
  144. });
  145. }
  146. //sdk自带获取位置方法(只支持微信环境),这里只当提示在用了(具体获取地址逻辑上面几个方法已完成)
  147. function wxGetLocation(successCallback, failCallback) {
  148. // #ifdef MP-WEIXIN
  149. // #endif
  150. console.log('wx.getLocation');
  151. // wx.getLocation({
  152. // type: 'gcj02',
  153. // isHighAccuracy: true,
  154. // highAccuracyExpireTime: 2000,
  155. // success(res) {},
  156. // fail(err) {
  157. // if(err.errMsg == 'getLocation:gps closed'){
  158. // uni.showToast({
  159. // title: '请打开GPS定位,否则定位不准确',
  160. // icon: 'none'
  161. // })
  162. // }
  163. // }
  164. // })
  165. }
  166. export default {
  167. calculateDistance, //计算两点距离
  168. getLocationDetail, //获取用户详细地址
  169. getLocation, //获取用户经纬度
  170. selectAddress, //打开地图让用户选择位置
  171. wxGetLocation,
  172. }