index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. },
  24. /**
  25. * 生命周期函数--监听页面加载
  26. */
  27. onLoad: function (options) {
  28. // 计算
  29. const systemInfo = wx.getSystemInfoSync();
  30. const statusBarHeight = systemInfo.statusBarHeight || 0;
  31. const screenHeight = systemInfo.screenHeight || 0;
  32. const custom = wx.getMenuButtonBoundingClientRect();
  33. const customHeight = custom.height + 10 + 2 || 0;
  34. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  35. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  36. const contentHeight = (screenHeight - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  37. this.setData({
  38. statusBarHeight,
  39. screenHeight,
  40. customHeight,
  41. bottomNavHeight,
  42. contentpadding,
  43. contentHeight
  44. });
  45. this.setData({
  46. title:options.id
  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. stop(){
  151. this.setData({
  152. isstop:!this.data.isstop
  153. })
  154. console.log(this.data.isstop);
  155. },
  156. })