index.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // nova-werun/pages/home/sport/sport-home/index.js
  2. const Parse = getApp().Parse;
  3. const company = getApp().globalData.company;
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. //屏幕高度
  10. statusBarHeight: 0, // 状态栏高度
  11. screenHeight: 0, // 屏幕高度
  12. customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮)
  13. bottomNavHeight: 0, // 底部导航栏高度
  14. contentHeight: 0, // 可用内容高度
  15. contentHeight2: 0,
  16. contentpadding: 0, //顶部padding高度
  17. //
  18. currentTab: 0,
  19. //地图
  20. longitude: 0,
  21. latitude: 0,
  22. markers: [],
  23. //
  24. distance:0
  25. },
  26. /**
  27. * 生命周期函数--监听页面加载
  28. */
  29. onLoad: function (options) {
  30. // 计算
  31. const systemInfo = wx.getSystemInfoSync();
  32. const statusBarHeight = systemInfo.statusBarHeight || 0;
  33. const screenHeight = systemInfo.screenHeight || 0;
  34. const custom = wx.getMenuButtonBoundingClientRect();
  35. const customHeight = custom.height + 10 + 2 || 0;
  36. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  37. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  38. const contentHeight = (screenHeight - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  39. this.setData({
  40. statusBarHeight,
  41. screenHeight,
  42. customHeight,
  43. bottomNavHeight,
  44. contentpadding,
  45. contentHeight,
  46. });
  47. if(Number(options.id)){
  48. this.setData({
  49. active:Number(options.id)
  50. })
  51. }
  52. //地图
  53. this.Getlocation()
  54. this.getwalk(0)
  55. },
  56. /**
  57. * 生命周期函数--监听页面初次渲染完成
  58. */
  59. onReady: function () {
  60. },
  61. /**
  62. * 生命周期函数--监听页面显示
  63. */
  64. onShow: function () {
  65. },
  66. /**
  67. * 生命周期函数--监听页面隐藏
  68. */
  69. onHide: function () {
  70. },
  71. /**
  72. * 生命周期函数--监听页面卸载
  73. */
  74. onUnload: function () {
  75. },
  76. /**
  77. * 页面相关事件处理函数--监听用户下拉动作
  78. */
  79. onPullDownRefresh: function () {
  80. },
  81. /**
  82. * 页面上拉触底事件的处理函数
  83. */
  84. onReachBottom: function () {
  85. },
  86. /**
  87. * 用户点击右上角分享
  88. */
  89. onShareAppMessage: function () {
  90. },
  91. //获取当前位置信息
  92. Getlocation() {
  93. // 获取当前位置信息
  94. wx.getLocation({
  95. type: 'wgs84',
  96. success: (res) => {
  97. const {
  98. latitude,
  99. longitude
  100. } = res;
  101. console.log('获取到的经纬度:', latitude, longitude); // 添加日志
  102. //调用api解析地址
  103. wx.request({
  104. url: 'https://api.map.baidu.com/reverse_geocoding/v3/?ak=sHZTomd7grslfP7sPKB8tRgT49FK9TEu&output=json&coordtype=gcj02&location=' + latitude + ',' + longitude,
  105. data: {},
  106. header: {
  107. 'Content-Type': 'application/json'
  108. },
  109. success: (ops) => { // 使用箭头函数
  110. console.log(ops);
  111. const address = ops.data.result.formatted_address;
  112. this.setData({
  113. address: address,
  114. latitude: latitude, // 保证 latitude 被设置
  115. longitude: longitude, // 保证 longitude 被设置
  116. markers: [{ // 设置 markers
  117. id: 1,
  118. latitude: latitude,
  119. longitude: longitude,
  120. iconPath: 'https://file-cloud.fmode.cn/13WZ0W7u3l/20240724/7ebg0k104325941.png?imageView2/1/w/200/h/200', // 自定义标记图标
  121. width: 40,
  122. height: 40,
  123. callout: {
  124. content: address, // 可以显示解析出的地址
  125. color: '#ffffff',
  126. bgColor: '#7F56B2',
  127. padding: 10,
  128. borderRadius: 5,
  129. display: 'ALWAYS'
  130. }
  131. }]
  132. });
  133. console.log(this.data.address);
  134. },
  135. fail: function (resq) {
  136. wx.showModal({
  137. title: '信息提示',
  138. content: '请求失败',
  139. showCancel: false,
  140. confirmColor: '#f37938'
  141. });
  142. },
  143. complete: function () {}
  144. })
  145. },
  146. fail: (err) => {
  147. console.error(err);
  148. wx.showToast({
  149. title: '获取位置失败',
  150. icon: 'none'
  151. });
  152. }
  153. });
  154. },
  155. //跳转
  156. gourl(e) {
  157. const url = e.currentTarget.dataset.url
  158. if(this.data.currentTab==0){
  159. const title='步行'
  160. wx.navigateTo({
  161. url: `${url}?id=`+title // 目标页面的路径
  162. });
  163. }else{
  164. const title='跑步'
  165. wx.navigateTo({
  166. url: `${url}?id=`+title // 目标页面的路径
  167. });
  168. }
  169. },
  170. // 经纬度点距离 gcj-02坐标系
  171. haversineDistance(location1, location2) {
  172. let lat1 = location1.latitude
  173. let lon1 = location1.longitude
  174. let lat2 = location2.latitude
  175. let lon2 = location2.longitude
  176. function toRad(degree) {
  177. return degree * Math.PI / 180;
  178. }
  179. const R = 6371e3; // 地球半径,单位为米
  180. const φ1 = toRad(lat1);
  181. const φ2 = toRad(lat2);
  182. const Δφ = toRad(lat2 - lat1);
  183. const Δλ = toRad(lon2 - lon1);
  184. const a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
  185. Math.cos(φ1) * Math.cos(φ2) *
  186. Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
  187. const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  188. const distance = R * c; // 距离,单位为米
  189. return distance;
  190. }
  191. //切换
  192. switchTab (e) {
  193. const index = e.currentTarget.dataset.index;
  194. this.setData({
  195. currentTab: index
  196. });
  197. this.getwalk(index)
  198. console.log(this.data.currentTab);
  199. },
  200. //获取行走公里
  201. async getwalk(index){
  202. const currentUser = Parse.User.current();
  203. let ActivityDataquery = new Parse.Query('ActivityData');
  204. ActivityDataquery.equalTo('user', currentUser.id);
  205. ActivityDataquery.equalTo('company', company);
  206. if(index==0){
  207. ActivityDataquery.equalTo('type', 'walk');
  208. }else{
  209. ActivityDataquery.equalTo('type', 'run');
  210. }
  211. ActivityDataquery.notEqualTo('isDeleted', true);
  212. let r = await ActivityDataquery.find();
  213. let List = r.map(item => item.toJSON());
  214. let distance = 0
  215. List.forEach(item=>{
  216. distance = Number(item.distance)+distance
  217. })
  218. this.setData({
  219. distance,
  220. })
  221. console.log('距离',this.data.distance);
  222. }
  223. })