铝交易,微信公众号
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.

183 lines
4.3 KiB

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