与牧同行-小程序用户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
4.9 KiB

6 days ago
6 days ago
6 days ago
6 days ago
6 days ago
6 days ago
6 days ago
6 days ago
  1. import http from '../../utils/api'
  2. const baseUr = require('../../utils/baseUrl')
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. longitude: null,
  9. latitude: null,
  10. markers: [],
  11. activeTab: 'clinic',
  12. baseUr: baseUr,
  13. showDetail: false,
  14. currentMarker: null
  15. },
  16. // 切换标签页
  17. switchTab(e) {
  18. const tab = e.currentTarget.dataset.tab;
  19. this.setData({
  20. activeTab: tab,
  21. showDetail: false // 切换标签时关闭详情
  22. });
  23. // 这里可以添加切换标签后的逻辑
  24. if (tab === 'clinic') {
  25. // 药店诊所相关操作
  26. console.log('切换到药店诊所');
  27. http.pharmacy({
  28. data: {},
  29. success: res => {
  30. console.log(111, res);
  31. this.setData({
  32. markers: res.rows.map((point, index) => ({
  33. id: point.id,
  34. title: point.title,
  35. latitude: point.latitude,
  36. longitude: point.longitude,
  37. iconPath: point.iconPath ? baseUr + point.iconPath : '/pages/images/dw.png',
  38. width: 35,
  39. height: 35,
  40. // 添加气泡标签显示名称
  41. callout: {
  42. content: point.title,
  43. color: '#333',
  44. fontSize: 12,
  45. borderRadius: 4,
  46. bgColor: '#ffffff',
  47. padding: 4,
  48. display: 'ALWAYS'
  49. },
  50. // 保存详细信息
  51. merchantType: point.merchantType,
  52. region: point.region,
  53. address: point.address
  54. }))
  55. })
  56. // 调整地图视野以显示所有标记
  57. if (res.rows.length > 0) {
  58. this.adjustMapView(res.rows);
  59. }
  60. }
  61. })
  62. } else if (tab === 'guide') {
  63. // 办事指南相关操作
  64. console.log('切换到办事指南');
  65. http.guidance({
  66. data: {},
  67. success: res => {
  68. console.log(222, res);
  69. // 清除地图标记
  70. this.setData({
  71. markers: []
  72. });
  73. }
  74. })
  75. }
  76. },
  77. // 调整地图视野以显示所有标记点
  78. adjustMapView(points) {
  79. const mapContext = wx.createMapContext('myMap', this);
  80. if (points && points.length > 0) {
  81. // 将点转换为地图需要的格式
  82. const mapPoints = points.map(point => ({
  83. latitude: point.latitude,
  84. longitude: point.longitude
  85. }));
  86. // 如果有当前位置,也包含在内
  87. if (this.data.latitude && this.data.longitude) {
  88. mapPoints.push({
  89. latitude: this.data.latitude,
  90. longitude: this.data.longitude
  91. });
  92. }
  93. // 调整地图视野
  94. mapContext.includePoints({
  95. points: mapPoints,
  96. padding: [60, 60, 60, 60]
  97. });
  98. }
  99. },
  100. // 地图标记点击事件
  101. onMarkerTap(e) {
  102. const markerId = e.markerId;
  103. const marker = this.data.markers.find(m => m.id === markerId);
  104. if (marker) {
  105. this.setData({
  106. showDetail: true,
  107. currentMarker: {
  108. title: marker.title,
  109. merchantType: marker.merchantType,
  110. region: marker.region,
  111. address: marker.address,
  112. latitude: marker.latitude,
  113. longitude: marker.longitude
  114. }
  115. });
  116. }
  117. },
  118. // 关闭详情
  119. closeDetail() {
  120. this.setData({
  121. showDetail: false,
  122. currentMarker: null
  123. });
  124. },
  125. // 导航到标记点
  126. navigateToMarker() {
  127. if (this.data.currentMarker) {
  128. const { latitude, longitude, title, address } = this.data.currentMarker;
  129. wx.openLocation({
  130. latitude: latitude,
  131. longitude: longitude,
  132. name: title,
  133. address: address,
  134. scale: 18
  135. });
  136. }
  137. },
  138. // 获取当前位置信息
  139. getlocation() {
  140. var that = this
  141. wx.getLocation({
  142. isHighAccuracy: true,
  143. type: 'gcj02',
  144. success: function (res) {
  145. console.log(res);
  146. const latitude = res.latitude;
  147. const longitude = res.longitude;
  148. that.setData({
  149. latitude: latitude,
  150. longitude: longitude
  151. });
  152. // 加载初始数据
  153. that.switchTab({ currentTarget: { dataset: { tab: 'clinic' } } });
  154. }
  155. })
  156. },
  157. /**
  158. * 生命周期函数--监听页面加载
  159. */
  160. onLoad(options) {
  161. this.getlocation()
  162. },
  163. /**
  164. * 生命周期函数--监听页面初次渲染完成
  165. */
  166. onReady() {
  167. },
  168. /**
  169. * 生命周期函数--监听页面显示
  170. */
  171. onShow() {
  172. },
  173. /**
  174. * 生命周期函数--监听页面隐藏
  175. */
  176. onHide() {
  177. this.setData({
  178. showDetail: false
  179. });
  180. },
  181. /**
  182. * 生命周期函数--监听页面卸载
  183. */
  184. onUnload() {
  185. },
  186. /**
  187. * 页面相关事件处理函数--监听用户下拉动作
  188. */
  189. onPullDownRefresh() {
  190. },
  191. /**
  192. * 页面上拉触底事件的处理函数
  193. */
  194. onReachBottom() {
  195. },
  196. /**
  197. * 用户点击右上角分享
  198. */
  199. onShareAppMessage() {
  200. }
  201. })