酒店桌布为微信小程序
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.

188 lines
4.4 KiB

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