index.js 5.0 KB

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