index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. /**
  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. //地图
  44. this.Getlocation()
  45. },
  46. /**
  47. * 生命周期函数--监听页面初次渲染完成
  48. */
  49. onReady: function () {
  50. },
  51. /**
  52. * 生命周期函数--监听页面显示
  53. */
  54. onShow: function () {
  55. },
  56. /**
  57. * 生命周期函数--监听页面隐藏
  58. */
  59. onHide: function () {
  60. },
  61. /**
  62. * 生命周期函数--监听页面卸载
  63. */
  64. onUnload: function () {
  65. },
  66. /**
  67. * 页面相关事件处理函数--监听用户下拉动作
  68. */
  69. onPullDownRefresh: function () {
  70. },
  71. /**
  72. * 页面上拉触底事件的处理函数
  73. */
  74. onReachBottom: function () {
  75. },
  76. /**
  77. * 用户点击右上角分享
  78. */
  79. onShareAppMessage: function () {
  80. },
  81. //获取当前位置信息
  82. Getlocation() {
  83. // 获取当前位置信息
  84. wx.getLocation({
  85. type: 'wgs84',
  86. success: (res) => {
  87. const {
  88. latitude,
  89. longitude
  90. } = res;
  91. console.log('获取到的经纬度:', latitude, longitude); // 添加日志
  92. //调用api解析地址
  93. wx.request({
  94. url: 'https://api.map.baidu.com/reverse_geocoding/v3/?ak=sHZTomd7grslfP7sPKB8tRgT49FK9TEu&output=json&coordtype=gcj02&location=' + latitude + ',' + longitude,
  95. data: {},
  96. header: {
  97. 'Content-Type': 'application/json'
  98. },
  99. success: (ops) => { // 使用箭头函数
  100. console.log(ops);
  101. const address = ops.data.result.formatted_address;
  102. this.setData({
  103. address: address,
  104. latitude: latitude, // 保证 latitude 被设置
  105. longitude: longitude, // 保证 longitude 被设置
  106. markers: [{ // 设置 markers
  107. id: 1,
  108. latitude: latitude,
  109. longitude: longitude,
  110. iconPath: 'https://file-cloud.fmode.cn/13WZ0W7u3l/20240724/7ebg0k104325941.png?imageView2/1/w/200/h/200', // 自定义标记图标
  111. width: 40,
  112. height: 40,
  113. callout: {
  114. content: address, // 可以显示解析出的地址
  115. color: '#ffffff',
  116. bgColor: '#7F56B2',
  117. padding: 10,
  118. borderRadius: 5,
  119. display: 'ALWAYS'
  120. }
  121. }]
  122. });
  123. console.log(this.data.address);
  124. },
  125. fail: function (resq) {
  126. wx.showModal({
  127. title: '信息提示',
  128. content: '请求失败',
  129. showCancel: false,
  130. confirmColor: '#f37938'
  131. });
  132. },
  133. complete: function () {}
  134. })
  135. },
  136. fail: (err) => {
  137. console.error(err);
  138. wx.showToast({
  139. title: '获取位置失败',
  140. icon: 'none'
  141. });
  142. }
  143. });
  144. },
  145. stop(){
  146. this.setData({
  147. isstop:!this.data.isstop
  148. })
  149. console.log(this.data.isstop);
  150. },
  151. })