index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // nova-werun/pages/home/sport/sport-start/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. longitude: 0,
  17. latitude: 0,
  18. markers: [],
  19. //是否暂停
  20. isstop:false,
  21. //标题
  22. title:'',
  23. percentage: '',
  24. timer: null,
  25. startTime: 0,
  26. },
  27. /**
  28. * 生命周期函数--监听页面加载
  29. */
  30. onLoad: function (options) {
  31. // 计算
  32. const systemInfo = wx.getSystemInfoSync();
  33. const statusBarHeight = systemInfo.statusBarHeight || 0;
  34. const screenHeight = systemInfo.screenHeight || 0;
  35. const custom = wx.getMenuButtonBoundingClientRect();
  36. const customHeight = custom.height + 10 + 2 || 0;
  37. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  38. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  39. const contentHeight = (screenHeight - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  40. this.setData({
  41. statusBarHeight,
  42. screenHeight,
  43. customHeight,
  44. bottomNavHeight,
  45. contentpadding,
  46. contentHeight
  47. });
  48. this.setData({
  49. title:options.id
  50. })
  51. //地图
  52. this.Getlocation()
  53. },
  54. /**
  55. * 生命周期函数--监听页面初次渲染完成
  56. */
  57. onReady: function () {
  58. },
  59. /**
  60. * 生命周期函数--监听页面显示
  61. */
  62. onShow: function () {
  63. },
  64. /**
  65. * 生命周期函数--监听页面隐藏
  66. */
  67. onHide: function () {
  68. },
  69. /**
  70. * 生命周期函数--监听页面卸载
  71. */
  72. onUnload: function () {
  73. },
  74. /**
  75. * 页面相关事件处理函数--监听用户下拉动作
  76. */
  77. onPullDownRefresh: function () {
  78. },
  79. /**
  80. * 页面上拉触底事件的处理函数
  81. */
  82. onReachBottom: function () {
  83. },
  84. /**
  85. * 用户点击右上角分享
  86. */
  87. onShareAppMessage: function () {
  88. },
  89. //获取当前位置信息
  90. Getlocation() {
  91. // 获取当前位置信息
  92. wx.getLocation({
  93. type: 'wgs84',
  94. success: (res) => {
  95. const {
  96. latitude,
  97. longitude
  98. } = res;
  99. console.log('获取到的经纬度:', latitude, longitude); // 添加日志
  100. //调用api解析地址
  101. wx.request({
  102. url: 'https://api.map.baidu.com/reverse_geocoding/v3/?ak=sHZTomd7grslfP7sPKB8tRgT49FK9TEu&output=json&coordtype=gcj02&location=' + latitude + ',' + longitude,
  103. data: {},
  104. header: {
  105. 'Content-Type': 'application/json'
  106. },
  107. success: (ops) => { // 使用箭头函数
  108. console.log(ops);
  109. const address = ops.data.result.formatted_address;
  110. this.setData({
  111. address: address,
  112. latitude: latitude, // 保证 latitude 被设置
  113. longitude: longitude, // 保证 longitude 被设置
  114. markers: [{ // 设置 markers
  115. id: 1,
  116. latitude: latitude,
  117. longitude: longitude,
  118. iconPath: 'https://file-cloud.fmode.cn/13WZ0W7u3l/20240724/7ebg0k104325941.png?imageView2/1/w/200/h/200', // 自定义标记图标
  119. width: 40,
  120. height: 40,
  121. callout: {
  122. content: address, // 可以显示解析出的地址
  123. color: '#ffffff',
  124. bgColor: '#7F56B2',
  125. padding: 10,
  126. borderRadius: 5,
  127. display: 'ALWAYS'
  128. }
  129. }]
  130. });
  131. console.log(this.data.address);
  132. },
  133. fail: function (resq) {
  134. wx.showModal({
  135. title: '信息提示',
  136. content: '请求失败',
  137. showCancel: false,
  138. confirmColor: '#f37938'
  139. });
  140. },
  141. complete: function () {}
  142. })
  143. },
  144. fail: (err) => {
  145. console.error(err);
  146. wx.showToast({
  147. title: '获取位置失败',
  148. icon: 'none'
  149. });
  150. }
  151. });
  152. },
  153. stop(){
  154. this.setData({
  155. isstop:!this.data.isstop
  156. })
  157. console.log(this.data.isstop);
  158. },
  159. startIncrease() {
  160. // 记录开始时间
  161. this.setData({
  162. startTime: Date.now(),
  163. });
  164. // 清除之前的定时器
  165. if (this.data.timer) {
  166. clearInterval(this.data.timer);
  167. }
  168. // 设置定时器,每隔 40 毫秒更新一次 percentage
  169. this.setData({
  170. timer: setInterval(() => {
  171. const currentTime = Date.now();
  172. const elapsedTime = currentTime - this.data.startTime;
  173. const percentage = Math.min((elapsedTime / 4000) * 100, 100);
  174. this.setData({
  175. percentage: `conic-gradient(from 0deg, #015EEA ${percentage}%, white 0%)`,
  176. });
  177. // 如果达到 100%,清除定时器
  178. if (percentage >= 100) {
  179. clearInterval(this.data.timer);
  180. this.setData({
  181. timer: null,
  182. });
  183. }
  184. }, 40),
  185. });
  186. },
  187. stopIncrease() {
  188. // 清除定时器
  189. if (this.data.timer) {
  190. clearInterval(this.data.timer);
  191. this.setData({
  192. timer: null,
  193. });
  194. // 如果未达到 100%,清零 percentage
  195. const elapsedTime = Date.now() - this.data.startTime;
  196. if (elapsedTime < 4000) {
  197. this.setData({
  198. percentage: '',
  199. });
  200. }
  201. }
  202. },
  203. })