与牧同行-小程序用户端
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.

262 lines
5.7 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months 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: null,
  12. baseUr: baseUr,
  13. showDetail: false,
  14. currentMarker: null
  15. },
  16. // 切换标签页
  17. switchTab(e) {
  18. const tab = e.currentTarget.dataset.tab;
  19. console.log(3333, e);
  20. this.setData({
  21. activeTab: tab,
  22. showDetail: false // 切换标签时关闭详情
  23. });
  24. // 这里可以添加切换标签后的逻辑
  25. if (tab === 'clinic') {
  26. // 药店诊所相关操作
  27. console.log('切换到药店诊所');
  28. http.pharmacy({
  29. data: {},
  30. success: 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: '#fff',
  44. fontSize: 12,
  45. borderRadius: 6,
  46. bgColor: '#2B5CA9',
  47. padding: 5,
  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. this.setData({
  70. markers: res.rows.map((point, index) => ({
  71. id: point.id,
  72. title: point.title,
  73. latitude: point.latitude,
  74. longitude: point.longitude,
  75. iconPath: point.iconPath ? baseUr + point.iconPath : '/pages/images/dw.png',
  76. width: 35,
  77. height: 35,
  78. // 添加气泡标签显示名称
  79. callout: {
  80. content: point.title,
  81. color: '#fff',
  82. fontSize: 12,
  83. borderRadius: 6,
  84. bgColor: '#2B5CA9',
  85. padding: 4,
  86. display: 'ALWAYS'
  87. },
  88. // 保存详细信息
  89. merchantType: point.guideType,
  90. region: point.region,
  91. address: point.address
  92. }))
  93. })
  94. // 调整地图视野以显示所有标记
  95. if (res.rows.length > 0) {
  96. this.adjustMapView(res.rows);
  97. }
  98. }
  99. })
  100. }
  101. },
  102. // 调整地图视野以显示所有标记点
  103. adjustMapView(points) {
  104. const mapContext = wx.createMapContext('myMap', this);
  105. if (points && points.length > 0) {
  106. // 将点转换为地图需要的格式
  107. const mapPoints = points.map(point => ({
  108. latitude: point.latitude,
  109. longitude: point.longitude
  110. }));
  111. // 如果有当前位置,也包含在内
  112. if (this.data.latitude && this.data.longitude) {
  113. mapPoints.push({
  114. latitude: this.data.latitude,
  115. longitude: this.data.longitude
  116. });
  117. }
  118. // 调整地图视野
  119. mapContext.includePoints({
  120. points: mapPoints,
  121. padding: [60, 60, 60, 60]
  122. });
  123. }
  124. },
  125. // 地图标记点击事件
  126. onMarkerTap(e) {
  127. const markerId = e.markerId;
  128. const marker = this.data.markers.find(m => m.id === markerId);
  129. if (marker) {
  130. this.setData({
  131. showDetail: true,
  132. currentMarker: {
  133. title: marker.title,
  134. merchantType: marker.merchantType,
  135. region: marker.region,
  136. address: marker.address,
  137. latitude: marker.latitude,
  138. longitude: marker.longitude
  139. }
  140. });
  141. }
  142. },
  143. // 关闭详情
  144. closeDetail() {
  145. this.setData({
  146. showDetail: false,
  147. currentMarker: null
  148. });
  149. },
  150. // 导航到标记点
  151. navigateToMarker() {
  152. if (this.data.currentMarker) {
  153. const {
  154. latitude,
  155. longitude,
  156. title,
  157. address
  158. } = this.data.currentMarker;
  159. wx.openLocation({
  160. latitude: latitude,
  161. longitude: longitude,
  162. name: title,
  163. address: address,
  164. scale: 18
  165. });
  166. }
  167. },
  168. // 获取当前位置信息
  169. getlocation() {
  170. var that = this
  171. wx.getLocation({
  172. isHighAccuracy: true,
  173. type: 'gcj02',
  174. success: function (res) {
  175. console.log(res);
  176. const latitude = res.latitude;
  177. const longitude = res.longitude;
  178. that.setData({
  179. latitude: latitude,
  180. longitude: longitude
  181. });
  182. }
  183. })
  184. },
  185. /**
  186. * 生命周期函数--监听页面加载
  187. */
  188. onLoad(options) {
  189. this.getlocation()
  190. },
  191. /**
  192. * 生命周期函数--监听页面初次渲染完成
  193. */
  194. onReady() {
  195. },
  196. /**
  197. * 生命周期函数--监听页面显示
  198. */
  199. onShow() {
  200. },
  201. /**
  202. * 生命周期函数--监听页面隐藏
  203. */
  204. onHide() {
  205. this.setData({
  206. showDetail: false
  207. });
  208. },
  209. /**
  210. * 生命周期函数--监听页面卸载
  211. */
  212. onUnload() {
  213. },
  214. /**
  215. * 页面相关事件处理函数--监听用户下拉动作
  216. */
  217. onPullDownRefresh() {
  218. },
  219. /**
  220. * 页面上拉触底事件的处理函数
  221. */
  222. onReachBottom() {
  223. },
  224. /**
  225. * 用户点击右上角分享
  226. */
  227. onShareAppMessage() {
  228. }
  229. })