合伙人运营小程序
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

index.js 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. Page({
  2. data: {
  3. // 地图基础配置
  4. longitude: 121.473701, // 默认上海经度
  5. latitude: 31.230416, // 默认上海纬度
  6. scale: 10, // 默认缩放级别
  7. markers: [], // 所有点位标记(车+电站+用电点)
  8. activeType: 'car', // 当前选中的选项卡类型
  9. currentList: [], // 当前显示的列表数据
  10. // 全量点位数据(实际项目替换为接口请求)
  11. pointData: {
  12. // 储能车数据
  13. car: [
  14. {
  15. id: 'car001',
  16. type: 'car',
  17. name: '储能车SC2026001',
  18. address: '上海市浦东新区张江高科技园区',
  19. longitude: 121.473701,
  20. latitude: 31.230416,
  21. status: '运行中'
  22. },
  23. {
  24. id: 'car002',
  25. type: 'car',
  26. name: '储能车SC2026002',
  27. address: '上海市徐汇区漕河泾开发区',
  28. longitude: 121.443875,
  29. latitude: 31.199337,
  30. status: '充电中'
  31. },
  32. {
  33. id: 'car003',
  34. type: 'car',
  35. name: '储能车SC2026003',
  36. address: '上海市闵行区紫竹高新区',
  37. longitude: 121.402099,
  38. latitude: 31.074742,
  39. status: '异常'
  40. }
  41. ],
  42. // 电站数据
  43. station: [
  44. {
  45. id: 'station001',
  46. type: 'station',
  47. name: '张江电站',
  48. address: '上海市浦东新区张江路',
  49. longitude: 121.482000,
  50. latitude: 31.235000,
  51. status: '正常运行'
  52. },
  53. {
  54. id: 'station002',
  55. type: 'station',
  56. name: '漕河泾电站',
  57. address: '上海市徐汇区虹漕路',
  58. longitude: 121.440000,
  59. latitude: 31.205000,
  60. status: '正常运行'
  61. },
  62. {
  63. id: 'station003',
  64. type: 'station',
  65. name: '青浦电站',
  66. address: '上海市青浦区华徐公路',
  67. longitude: 121.165000,
  68. latitude: 31.158000,
  69. status: '正常运行'
  70. }
  71. ],
  72. // 用电点数据
  73. user: [
  74. {
  75. id: 'user001',
  76. type: 'user',
  77. name: '陆家嘴商业中心',
  78. address: '上海市浦东新区陆家嘴环路',
  79. longitude: 121.506377,
  80. latitude: 31.238039,
  81. status: '用电正常'
  82. },
  83. {
  84. id: 'user002',
  85. type: 'user',
  86. name: '人民广场商圈',
  87. address: '上海市黄浦区人民大道',
  88. longitude: 121.473081,
  89. latitude: 31.230664,
  90. status: '用电正常'
  91. },
  92. {
  93. id: 'user003',
  94. type: 'user',
  95. name: '虹桥商务区',
  96. address: '上海市闵行区申虹路',
  97. longitude: 121.328000,
  98. latitude: 31.198000,
  99. status: '用电正常'
  100. }
  101. ]
  102. },
  103. // 标记点图标配置(替换为你的实际图标路径)
  104. markerIcons: {
  105. car: '/images/car.png', // 储能车图标
  106. station: '/images/station.png', // 电站图标
  107. user: '/images/user.png' // 用电点图标
  108. },
  109. // 地图上下文(用于平滑操作)
  110. mapCtx: null
  111. },
  112. onLoad(options) {
  113. // 1. 创建地图上下文
  114. this.mapCtx = wx.createMapContext('map', this);
  115. // 2. 检查是否有定位参数(从详情页跳转回来)
  116. if (options.targetId && options.lng && options.lat) {
  117. this.smoothLocateToPoint({
  118. longitude: Number(options.lng),
  119. latitude: Number(options.lat)
  120. }, 17);
  121. }
  122. // 3. 初始化所有点位(地图永久显示所有标记)
  123. this.initAllMarkers();
  124. // 4. 初始化默认列表(储能车)
  125. this.switchType({ currentTarget: { dataset: { type: 'car' } } });
  126. },
  127. /**
  128. * 初始化所有点位:合并+平滑适配视野+生成标记
  129. */
  130. initAllMarkers() {
  131. // 合并所有三类点位
  132. const allPoints = [
  133. ...this.data.pointData.car,
  134. ...this.data.pointData.station,
  135. ...this.data.pointData.user
  136. ];
  137. // 生成所有标记点
  138. this.generateAllMarkers(allPoints);
  139. // 平滑适配所有点位视野
  140. this.smoothFitAllPoints(allPoints);
  141. },
  142. /**
  143. * 平滑适配所有点位视野(核心)
  144. * @param {Array} pointList 所有点位列表
  145. */
  146. smoothFitAllPoints(pointList) {
  147. if (!pointList || pointList.length === 0) return;
  148. // 格式化点位数据
  149. const points = pointList.map(item => ({
  150. longitude: Number(item.longitude),
  151. latitude: Number(item.latitude)
  152. })).filter(v => !isNaN(v.longitude) && !isNaN(v.latitude));
  153. // 调用地图原生方法:平滑包含所有点位
  154. this.mapCtx.includePoints({
  155. points: points,
  156. padding: [60, 60, 60, 60], // 边距,避免遮挡
  157. animation: true // 平滑动画
  158. });
  159. // 同步地图数据到页面
  160. this.syncMapData();
  161. },
  162. /**
  163. * 生成所有三类点位的标记点
  164. * @param {Array} pointList 所有点位列表
  165. */
  166. generateAllMarkers(pointList) {
  167. if (!pointList || pointList.length === 0) {
  168. this.setData({ markers: [] });
  169. return;
  170. }
  171. const markers = pointList.map((item, index) => ({
  172. id: index + 1, // 唯一ID
  173. longitude: Number(item.longitude),
  174. latitude: Number(item.latitude),
  175. iconPath: this.data.markerIcons[item.type] || '/images/car.png',
  176. width: 40,
  177. height: 40,
  178. anchor: { x: 0.5, y: 0.5 }, // 图标中心对准点位
  179. // 点击弹窗详情
  180. callout: {
  181. content: `${item.name}\n${item.address}${item.status ? '\n状态:' + item.status : ''}`,
  182. fontSize: 24,
  183. borderRadius: 8,
  184. bgColor: '#fff',
  185. padding: 12,
  186. display: 'BYCLICK',
  187. textAlign: 'left'
  188. },
  189. // 绑定原始数据(用于点击事件)
  190. customData: item
  191. }));
  192. this.setData({ markers });
  193. },
  194. /**
  195. * 平滑定位到单个点位(通用方法)
  196. * @param {Object} point 点位(含longitude/latitude)
  197. * @param {Number} targetScale 目标缩放级别
  198. */
  199. smoothLocateToPoint(point, targetScale = 17) {
  200. if (!point || isNaN(point.longitude) || isNaN(point.latitude)) return;
  201. // 平滑移动地图中心
  202. this.mapCtx.translateMap({
  203. longitude: point.longitude - this.data.longitude,
  204. latitude: point.latitude - this.data.latitude,
  205. duration: 500,
  206. animationEnd: () => {
  207. // 移动完成后平滑缩放
  208. this.mapCtx.setScale({
  209. scale: targetScale,
  210. animation: true,
  211. duration: 300
  212. });
  213. }
  214. });
  215. // 更新页面数据
  216. this.setData({
  217. longitude: point.longitude,
  218. latitude: point.latitude,
  219. scale: targetScale
  220. });
  221. },
  222. /**
  223. * 同步地图数据到页面(解决数据不一致)
  224. */
  225. syncMapData() {
  226. // 获取当前中心经纬度
  227. this.mapCtx.getCenterLocation({
  228. success: (res) => {
  229. this.setData({
  230. longitude: res.longitude,
  231. latitude: res.latitude
  232. });
  233. }
  234. });
  235. // 获取当前缩放级别
  236. this.mapCtx.getScale({
  237. success: (res) => {
  238. this.setData({ scale: res.scale });
  239. }
  240. });
  241. },
  242. /**
  243. * 切换选项卡:仅更新列表,不影响地图
  244. */
  245. switchType(e) {
  246. const type = e.currentTarget.dataset.type;
  247. this.setData({
  248. activeType: type,
  249. currentList: this.data.pointData[type] || []
  250. });
  251. },
  252. /**
  253. * 列表项点击:跳转至详情页
  254. */
  255. toDetailPage(e) {
  256. const item = e.currentTarget.dataset.item;
  257. if (!item.id || !item.type) return;
  258. // 跳转详情页(替换为你的详情页实际路径)
  259. wx.navigateTo({
  260. url: `/pages/station-detail/station-detail?id=${item.id}&type=${item.type}`
  261. });
  262. },
  263. /**
  264. * 标记点点击:平滑定位到该点位
  265. */
  266. onMarkerTap(e) {
  267. const marker = this.data.markers.find(item => item.id === e.detail.markerId);
  268. if (marker && marker.customData) {
  269. this.smoothLocateToPoint(marker.customData, 18);
  270. }
  271. },
  272. /**
  273. * 地图视野变化监听
  274. */
  275. onRegionChange(e) {
  276. if (e.type === 'end') {
  277. this.syncMapData();
  278. }
  279. },
  280. /**
  281. * 页面卸载:销毁地图上下文
  282. */
  283. onUnload() {
  284. this.mapCtx = null;
  285. }
  286. });