index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // nova-werun/pages/home/sport/sport-home/index.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. //屏幕高度
  8. statusBarHeight: 0, // 状态栏高度
  9. screenHeight: 0, // 屏幕高度
  10. customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮)
  11. bottomNavHeight: 0, // 底部导航栏高度
  12. contentHeight: 0, // 可用内容高度
  13. contentHeight2: 0,
  14. contentpadding: 0, //顶部padding高度
  15. //
  16. active: 0,
  17. //地图
  18. longitude: 0,
  19. latitude: 0,
  20. markers: [],
  21. },
  22. /**
  23. * 生命周期函数--监听页面加载
  24. */
  25. onLoad: function (options) {
  26. // 计算
  27. const systemInfo = wx.getSystemInfoSync();
  28. const statusBarHeight = systemInfo.statusBarHeight || 0;
  29. const screenHeight = systemInfo.screenHeight || 0;
  30. const custom = wx.getMenuButtonBoundingClientRect();
  31. const customHeight = custom.height + 10 + 2 || 0;
  32. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  33. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  34. const contentHeight = (screenHeight - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  35. this.setData({
  36. statusBarHeight,
  37. screenHeight,
  38. customHeight,
  39. bottomNavHeight,
  40. contentpadding,
  41. contentHeight,
  42. });
  43. if(Number(options.id)){
  44. this.setData({
  45. active:Number(options.id)
  46. })
  47. }
  48. //地图
  49. this.Getlocation()
  50. },
  51. /**
  52. * 生命周期函数--监听页面初次渲染完成
  53. */
  54. onReady: function () {
  55. },
  56. /**
  57. * 生命周期函数--监听页面显示
  58. */
  59. onShow: function () {
  60. },
  61. /**
  62. * 生命周期函数--监听页面隐藏
  63. */
  64. onHide: function () {
  65. },
  66. /**
  67. * 生命周期函数--监听页面卸载
  68. */
  69. onUnload: function () {
  70. },
  71. /**
  72. * 页面相关事件处理函数--监听用户下拉动作
  73. */
  74. onPullDownRefresh: function () {
  75. },
  76. /**
  77. * 页面上拉触底事件的处理函数
  78. */
  79. onReachBottom: function () {
  80. },
  81. /**
  82. * 用户点击右上角分享
  83. */
  84. onShareAppMessage: function () {
  85. },
  86. //获取当前位置信息
  87. Getlocation() {
  88. // 获取当前位置信息
  89. wx.getLocation({
  90. type: 'wgs84',
  91. success: (res) => {
  92. const {
  93. latitude,
  94. longitude
  95. } = res;
  96. console.log('获取到的经纬度:', latitude, longitude); // 添加日志
  97. //调用api解析地址
  98. wx.request({
  99. url: 'https://api.map.baidu.com/reverse_geocoding/v3/?ak=sHZTomd7grslfP7sPKB8tRgT49FK9TEu&output=json&coordtype=gcj02&location=' + latitude + ',' + longitude,
  100. data: {},
  101. header: {
  102. 'Content-Type': 'application/json'
  103. },
  104. success: (ops) => { // 使用箭头函数
  105. console.log(ops);
  106. const address = ops.data.result.formatted_address;
  107. this.setData({
  108. address: address,
  109. latitude: latitude, // 保证 latitude 被设置
  110. longitude: longitude, // 保证 longitude 被设置
  111. markers: [{ // 设置 markers
  112. id: 1,
  113. latitude: latitude,
  114. longitude: longitude,
  115. iconPath: 'https://file-cloud.fmode.cn/13WZ0W7u3l/20240724/7ebg0k104325941.png?imageView2/1/w/200/h/200', // 自定义标记图标
  116. width: 40,
  117. height: 40,
  118. callout: {
  119. content: address, // 可以显示解析出的地址
  120. color: '#ffffff',
  121. bgColor: '#7F56B2',
  122. padding: 10,
  123. borderRadius: 5,
  124. display: 'ALWAYS'
  125. }
  126. }]
  127. });
  128. console.log(this.data.address);
  129. },
  130. fail: function (resq) {
  131. wx.showModal({
  132. title: '信息提示',
  133. content: '请求失败',
  134. showCancel: false,
  135. confirmColor: '#f37938'
  136. });
  137. },
  138. complete: function () {}
  139. })
  140. },
  141. fail: (err) => {
  142. console.error(err);
  143. wx.showToast({
  144. title: '获取位置失败',
  145. icon: 'none'
  146. });
  147. }
  148. });
  149. },
  150. //跳转
  151. gourl(e) {
  152. const url = e.currentTarget.dataset.url
  153. if(this.data.active==0){
  154. const title='步行'
  155. wx.navigateTo({
  156. url: `${url}?id=`+title // 目标页面的路径
  157. });
  158. }else{
  159. const title='跑步'
  160. wx.navigateTo({
  161. url: `${url}?id=`+title // 目标页面的路径
  162. });
  163. }
  164. },
  165. })