player.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. // import L from 'leaflet'
  2. import config from '@/utils/map/sub/config'
  3. import global from '@/utils/map/sub/global'
  4. import store from '@/store/index'
  5. import {
  6. formatTime
  7. } from "@/utils/util.js"
  8. // var interval_creatCircleMarker = null
  9. // var interval_showTrail = null
  10. var togglePlayerFlag = store.state.mapControlPlayer
  11. var toggleTooltipFlag = store.state.mapControlTooltip
  12. var toggleTrailFlag = store.state.mapControlTrail
  13. export default {
  14. playerLayerGroup: null, // 玩家标记图层组
  15. trailLayerGroup: null, // 玩家轨迹图层组
  16. init() {
  17. this.playerLayerGroup = new L.layerGroup()
  18. this.trailLayerGroup = new L.layerGroup()
  19. },
  20. // 即将开始地图缩放时触发本方法
  21. onZoomStart(e) {
  22. if (togglePlayerFlag) {
  23. this.playerLayerGroup.remove()
  24. }
  25. if (toggleTrailFlag) {
  26. this.trailLayerGroup.remove()
  27. }
  28. },
  29. // 地图发生缩放时触发本方法
  30. onZoom(e) {},
  31. // 地图缩放结束时触发本方法
  32. onZoomEnd(e) {
  33. if (toggleTrailFlag) {
  34. this.trailLayerGroup.addTo(global.map)
  35. }
  36. if (togglePlayerFlag) {
  37. this.playerLayerGroup.addTo(global.map)
  38. }
  39. },
  40. togglePlayer(flag) {
  41. if (flag != null) {
  42. togglePlayerFlag = !flag
  43. }
  44. // console.log("[togglePlayer]", togglePlayerFlag)
  45. if (togglePlayerFlag) {
  46. this.playerLayerGroup.removeFrom(global.map)
  47. } else {
  48. this.playerLayerGroup.addTo(global.map)
  49. }
  50. togglePlayerFlag = !togglePlayerFlag
  51. store.commit('setMapControlPlayer', togglePlayerFlag)
  52. },
  53. toggleTooltip(flag) {
  54. if (flag != null) {
  55. toggleTooltipFlag = !flag
  56. } else {
  57. // if (!togglePlayerFlag) {
  58. // this.togglePlayer(true)
  59. // toggleTooltipFlag = false
  60. // }
  61. }
  62. for (let i = 0; i < global.storePlayers.length; i++) {
  63. var player = global.storePlayers[i]
  64. if (player != null && player.marker != null) {
  65. // player.marker.toggleTooltip()
  66. // let isopen = player.marker.isTooltipOpen()
  67. if (toggleTooltipFlag) {
  68. player.marker.closeTooltip()
  69. } else {
  70. player.marker.openTooltip()
  71. }
  72. }
  73. }
  74. toggleTooltipFlag = !toggleTooltipFlag
  75. store.commit('setMapControlTooltip', toggleTooltipFlag)
  76. },
  77. toggleTrail(flag) {
  78. if (flag != null) {
  79. toggleTrailFlag = !flag
  80. }
  81. if (toggleTrailFlag) {
  82. this.trailLayerGroup.removeFrom(global.map)
  83. } else {
  84. this.trailLayerGroup.addTo(global.map)
  85. }
  86. toggleTrailFlag = !toggleTrailFlag
  87. store.commit('setMapControlTrail', toggleTrailFlag)
  88. },
  89. drawAllPlayers() {
  90. // console.log('[drawAllPlayers]', global.players)
  91. // if (this.playerLayerGroup != null)
  92. // this.playerLayerGroup.clearLayers()
  93. for (let i = 0; i < global.players.length; i++) {
  94. this.drawOnePlayer(global.players[i].id)
  95. // this.drawOnePlayer2(global.players[i].id)
  96. }
  97. },
  98. drawOnePlayer2(playerId, animate = true) {
  99. var that = this
  100. var player = global.getPlayerById(playerId)
  101. var player_position = global.getPlayerPositionById(playerId)
  102. var storePlayer = global.getStorePlayersById(playerId)
  103. // console.log(player, player_position)
  104. if (player == null || player_position == null) {
  105. console.warn('[drawOnePlayer2] 关键数据为空', player, player_position)
  106. return
  107. }
  108. // 首次创建
  109. if (storePlayer == null || storePlayer.marker == null) {
  110. // 在地图上创建 marker 并存储用户信息
  111. var playerIcon = L.icon({
  112. iconUrl: 'static/image/marker-icon.png',
  113. iconSize: [20, 32.8],
  114. iconAnchor: [10, 30]
  115. });
  116. var marker = L.marker([player_position.latitude, player_position.longitude], {
  117. icon: playerIcon
  118. })
  119. .addTo(this.playerLayerGroup)
  120. // .addTo(global.map)
  121. // .bindPopup("Hello, I'm a Marker!<br><img src='my-image.png' width='100'>").openPopup();
  122. .bindTooltip(`${player.name}`, {
  123. permanent: true,
  124. offset: [0, -30],
  125. direction: 'top',
  126. interactive: true
  127. })
  128. marker.type = 'marker'
  129. marker.playerId = player.id // 存储用户信息
  130. marker.animate = animate
  131. marker.on('click', function(e) {
  132. var playerId = e.target.playerId; // 获取点击的 marker 的用户信息
  133. console.log("Selected player ID: " + playerId);
  134. that.handleFocusPlayer(playerId)
  135. });
  136. var tooltip = marker.getTooltip()
  137. tooltip.playerId = player.id // 存储用户信息
  138. tooltip.animate = animate
  139. tooltip.on('click', function(e) {
  140. // console.log("[Marker.Tooltip]", e)
  141. var playerId = e.target.playerId; // 获取点击的 marker 的用户信息
  142. console.log("[Marker.Tooltip] Selected player ID: " + playerId)
  143. that.handleFocusPlayer(playerId)
  144. })
  145. // this.playerLayerGroup.addTo(global.map)
  146. if (storePlayer == null) {
  147. storePlayer = {
  148. id: playerId,
  149. marker: marker, // 玩家标识
  150. markerLayerGroup: this.playerLayerGroup,
  151. trail: null, // 玩家轨迹
  152. trailData: [], // 玩家轨迹信息
  153. interval_creatCircleMarker: null,
  154. interval_showTrail: null,
  155. }
  156. global.storePlayers.push(storePlayer)
  157. } else {
  158. storePlayer.marker = marker
  159. storePlayer.markerLayerGroup = this.playerLayerGroup
  160. }
  161. }
  162. // 非首次创建,直接移动位置
  163. else {
  164. if (!toggleTrailFlag) {
  165. var marker = storePlayer.marker
  166. var curPoint = [player_position.latitude, player_position.longitude]
  167. // console.warn('[drawOnePlayer] 更新玩家的标记位置', storePlayer.marker, curPoint)
  168. marker.setLatLng(curPoint)
  169. }
  170. }
  171. },
  172. // 创建动画效果函数,用于实现circleMarker的大小和透明度随时间变化
  173. animateCircle(circle, minRadius, maxRadius, step = 1) {
  174. var radius = circle.getRadius();
  175. var opacity = circle.options.fillOpacity;
  176. var zoomType = 'zoomIn' // zoomIn: 放大 zoomOut: 缩小
  177. var interval_creatCircleMarker = setInterval(function() {
  178. if (!circle.animate) {
  179. clearInterval(interval_creatCircleMarker)
  180. return
  181. }
  182. // 改变圆形半径和透明度
  183. if (zoomType == 'zoomIn') {
  184. radius = radius + step
  185. // opacity = opacity - 0.1
  186. } else if (zoomType == 'zoomOut') {
  187. radius = radius - step
  188. // opacity = opacity - 0.1
  189. }
  190. circle.setRadius(radius);
  191. circle.setStyle({
  192. fillOpacity: opacity
  193. });
  194. if (radius >= maxRadius) {
  195. zoomType = 'zoomOut'
  196. } else if (radius <= minRadius) {
  197. zoomType = 'zoomIn'
  198. }
  199. }, 50);
  200. return interval_creatCircleMarker
  201. },
  202. getPopupContent(player) {
  203. var popupContent = '<div style="line-height: 20px;">'
  204. popupContent += '<div>' + ' <span style="color: blue; font-weight: bold;" onClick="uni.makePhoneCall({phoneNumber: \'' + player
  205. .phone + '\'});">' + player.name + '</span>'
  206. if (player.nextcontrolpoint.sn != '') {
  207. popupContent += ' &nbsp; <span style="font-weight: bold;">=> ' + player.nextcontrolpoint.sn + '</span>'
  208. }
  209. popupContent += ' &nbsp; 百味豆 ' + player.syspoint + ' </div>'
  210. popupContent += '<div>打点 ' + player.effectivenum + '/' + player.totalcontrolnum +
  211. ' &nbsp; 距离 ' + player.distance + '米 &nbsp; 配速 ' + formatTime(player.pace, false) + '</div>'
  212. popupContent += '<div>' + '心率 ' + player.lasthr + ' &nbsp; 平均 ' + player.avghr + ' &nbsp; 最大 ' + player.maxhr + '</div>'
  213. popupContent += '<div>Cal ' + Math.round(player.calorie / 1000) + ' &nbsp; Ck ' + Math.round(player.ck / 1000) +
  214. ' &nbsp; Ei ' + Math.round(player.ei * 100) / 100 + '</div>'
  215. popupContent += '</div>'
  216. return popupContent
  217. },
  218. drawOnePlayer(playerId, animate = true) {
  219. var that = this
  220. var player = global.getPlayerById(playerId)
  221. var player_position = global.getPlayerPositionById(playerId)
  222. var storePlayer = global.getStorePlayersById(playerId)
  223. // console.log('[drawOnePlayer]', player, player_position)
  224. if (player == null) {
  225. console.warn('[drawOnePlayer] 玩家数据为空', player)
  226. return
  227. }
  228. if (player_position == null) {
  229. // console.log('[drawOnePlayer] 玩家位置数据为空', player_position)
  230. return
  231. }
  232. var popupContent = this.getPopupContent(player)
  233. // 首次创建
  234. if (storePlayer == null || storePlayer.marker == null) {
  235. // console.log('[drawOnePlayer] storePlayer == null', playerId)
  236. var playerColor = this.getPlayerColor(playerId)
  237. var options = config.gStyle.marker.default
  238. options.fillColor = playerColor
  239. options.stroke = config.gStyle.common.stroke
  240. options.color = config.gStyle.common.color
  241. // console.log('[drawOnePlayer] options', options)
  242. var marker = L.circleMarker([player_position.latitude, player_position.longitude], options)
  243. .addTo(this.playerLayerGroup)
  244. .bindPopup(popupContent)
  245. .bindTooltip(`${player.name}`, config.gStyle.marker.tooltip)
  246. marker.type = 'circleMarker'
  247. marker.playerId = player.id // 存储用户信息
  248. marker.animate = animate
  249. marker.on('click', function(e) {
  250. // console.log("[Marker]", e)
  251. var playerId = e.target.playerId; // 获取点击的 marker 的用户信息
  252. console.log("[Marker] Selected player ID: " + playerId)
  253. that.handleFocusPlayer(playerId)
  254. })
  255. var tooltip = marker.getTooltip()
  256. tooltip.playerId = player.id // 存储用户信息
  257. tooltip.animate = animate
  258. tooltip.on('click', function(e) {
  259. // console.log("[Marker.Tooltip]", e)
  260. var playerId = e.target.playerId; // 获取点击的 marker 的用户信息
  261. console.log("[Marker.Tooltip] Selected player ID: " + playerId)
  262. that.handleFocusPlayer(playerId)
  263. })
  264. var interval_creatCircleMarker = null
  265. if (animate) {
  266. interval_creatCircleMarker = this.animateCircle(marker, 5, 10, 0.5)
  267. }
  268. // this.playerLayerGroup.addTo(global.map)
  269. if (storePlayer == null) {
  270. storePlayer = {
  271. id: playerId,
  272. marker: marker, // 玩家标识
  273. markerLayerGroup: this.playerLayerGroup,
  274. trail: null, // 玩家轨迹 外层描边轨迹
  275. trail2: null, // 玩家轨迹 内层填充轨迹
  276. trailLayerGroup: null,
  277. trailData: [], // 玩家轨迹信息
  278. interval_creatCircleMarker: interval_creatCircleMarker,
  279. interval_showTrail: null,
  280. }
  281. global.storePlayers.push(storePlayer)
  282. } else {
  283. storePlayer.marker = marker
  284. storePlayer.markerLayerGroup = this.playerLayerGroup
  285. storePlayer.interval_creatCircleMarker = interval_creatCircleMarker
  286. }
  287. }
  288. // 非首次创建,直接移动位置
  289. else {
  290. if (!toggleTrailFlag) {
  291. var marker = storePlayer.marker
  292. var curPoint = [player_position.latitude, player_position.longitude]
  293. // console.log('[drawOnePlayer] 更新玩家的标记位置', storePlayer.marker, curPoint)
  294. marker.setLatLng(curPoint)
  295. marker.bringToFront()
  296. marker.setPopupContent(popupContent)
  297. }
  298. }
  299. },
  300. handleFocusPlayer(playerId) {
  301. console.log("[handleFocusPlayer] 当前选中玩家ID: " + playerId)
  302. if (global.focusPlayerId > 0) {
  303. // 先把之前选中的目标恢复成默认状态
  304. var unfocusPlayer = global.getStorePlayersById(global.focusPlayerId)
  305. // console.log("unfocusPlayer", unfocusPlayer)
  306. var playerColor = this.getPlayerColor(global.focusPlayerId)
  307. if (unfocusPlayer != null && unfocusPlayer.marker != null && unfocusPlayer.marker.type == 'circleMarker') {
  308. var options = config.gStyle.marker.default
  309. options.fillColor = playerColor
  310. unfocusPlayer.marker.setStyle(options)
  311. }
  312. if (unfocusPlayer != null && unfocusPlayer.trail != null) {
  313. var options = config.gStyle.trail.default
  314. options.color = playerColor
  315. unfocusPlayer.trail2.setStyle(options)
  316. }
  317. }
  318. global.focusPlayerId = playerId
  319. global.getCaller().focusPlayerId = playerId
  320. var focusPlayer = global.getStorePlayersById(global.focusPlayerId)
  321. // console.log("focusPlayer", focusPlayer)
  322. if (focusPlayer != null && focusPlayer.trail != null) {
  323. var options = config.gStyle.trail.focus
  324. focusPlayer.trail2.setStyle(options)
  325. focusPlayer.trail.bringToFront()
  326. focusPlayer.trail2.bringToFront()
  327. }
  328. if (focusPlayer != null && focusPlayer.marker != null && focusPlayer.marker.type == 'circleMarker') {
  329. var options = config.gStyle.marker.focus
  330. options.stroke = config.gStyle.common.stroke
  331. options.color = config.gStyle.common.color
  332. focusPlayer.marker.setStyle(options)
  333. focusPlayer.marker.openPopup()
  334. focusPlayer.marker.bringToFront()
  335. }
  336. },
  337. drawAllTrails(duration) {
  338. // if (this.trailLayerGroup != null)
  339. // this.trailLayerGroup.clearLayers()
  340. for (let i = 0; i < global.players.length; i++) {
  341. this.drawOneTrail(global.players[i].id, duration)
  342. }
  343. },
  344. // 显示运动轨迹 duration:毫秒
  345. drawOneTrail(playerId, duration) {
  346. var player = global.getPlayerById(playerId)
  347. var player_position = global.getPlayerPositionById(playerId)
  348. var storePlayer = global.getStorePlayersById(playerId)
  349. // console.log('[drawOneTrail] param', player, player_position, storePlayer)
  350. if (player == null) {
  351. console.warn('[drawOneTrail] 玩家数据为空', player)
  352. return
  353. }
  354. if (player_position == null) {
  355. // console.log('[drawOneTrail] 玩家位置数据为空', player_position)
  356. return
  357. }
  358. if (storePlayer == null || storePlayer.trail == null) {
  359. // console.warn('[drawOneTrail] 轨迹数据为空', player, storePlayer)
  360. var trail = null
  361. var trail2 = null
  362. var curPoint = [player_position.latitude, player_position.longitude, new Date()]
  363. // console.log('[drawOneTrail] curPoint', curPoint)
  364. var trailData = null
  365. var options = config.gStyle.trail.default
  366. // 获取Vuex中暂存的用户数据,用于页面重载后的数据恢复
  367. var holdPlayerData = global.fetchPlayersDataById(playerId)
  368. // console.warn("[drawOneTrail] holdPlayerData", holdPlayerData)
  369. if (holdPlayerData != null && holdPlayerData.trailData != null && holdPlayerData.trailData.length > 0) {
  370. // console.warn("[drawOneTrail] 加载暂存轨迹数据", holdPlayerData.trailData)
  371. // concat方法连接a、b两个数组后,a、b两个数组的数据不变,同时会返回一个新的数组
  372. trailData = holdPlayerData.trailData.concat([curPoint])
  373. // console.warn("[drawOneTrail] 轨迹数据", trailData)
  374. } else {
  375. trailData = [curPoint]
  376. }
  377. // 外层描边轨迹
  378. if (config.gStyle.common.stroke) {
  379. options.weight = 6.9
  380. } else {
  381. options.weight = 0
  382. }
  383. options.color = config.gStyle.common.color
  384. trail = L.polyline(trailData, options)
  385. .addTo(this.trailLayerGroup)
  386. // console.log('[drawOneTrail] trail', trail)
  387. // 内层填充轨迹
  388. var playerColor = this.getPlayerColor(playerId)
  389. options.weight = 3.9
  390. options.color = playerColor
  391. // options.fillColor = playerColor
  392. trail2 = L.polyline(trailData, options)
  393. .addTo(this.trailLayerGroup)
  394. if (storePlayer == null) {
  395. storePlayer = {
  396. id: playerId,
  397. marker: null, // 玩家标识
  398. markerLayerGroup: null,
  399. trail: trail, // 玩家轨迹 外层描边轨迹
  400. trail2: trail2, // 玩家轨迹 内层填充轨迹
  401. trailLayerGroup: this.trailLayerGroup,
  402. trailData: trailData, // 玩家轨迹信息
  403. interval_creatCircleMarker: null,
  404. interval_showTrail: null,
  405. }
  406. global.storePlayers.push(storePlayer)
  407. } else {
  408. storePlayer.trail = trail
  409. storePlayer.trail2 = trail2
  410. storePlayer.trailLayerGroup = this.trailLayerGroup
  411. storePlayer.trailData = trailData
  412. }
  413. var that = this
  414. var interval_showTrail = setInterval(function() {
  415. player = global.getPlayerById(playerId)
  416. if (player == null) {
  417. console.warn('[drawOneTrail] 玩家数据为空 (已完赛或已退赛)', player)
  418. clearInterval(interval_showTrail)
  419. storePlayer.interval_showTrail = null
  420. return
  421. }
  422. // 去除过期的历史轨迹
  423. if (duration > 0) {
  424. var now = new Date()
  425. storePlayer.trailData = storePlayer.trailData.filter(function(point) {
  426. return (now - point[2]) < duration
  427. });
  428. }
  429. player_position = global.getPlayerPositionById(playerId)
  430. if (player_position == null || player_position.latitude == null || player_position.longitude == null) {
  431. console.warn('[drawOneTrail] 玩家位置信息为空 playerId:' + playerId)
  432. return
  433. }
  434. curPoint = [player_position.latitude, player_position.longitude, new Date()]
  435. storePlayer.trailData.push(curPoint)
  436. // storePlayer.trailData.push(curPoint.slice(0, 2))
  437. if (toggleTrailFlag) {
  438. // console.log('interval_showTrail', storePlayer.trailData)
  439. trail.setLatLngs(storePlayer.trailData);
  440. trail2.setLatLngs(storePlayer.trailData);
  441. if (storePlayer.marker != null) {
  442. if (toggleTrailFlag) {
  443. // console.warn('[drawOneTrail] 更新玩家的标记位置', storePlayer.marker, curPoint)
  444. storePlayer.marker.setLatLng(curPoint.slice(0, 2)) // 更新玩家的标记位置
  445. storePlayer.marker.bringToFront()
  446. var popupContent = that.getPopupContent(player)
  447. storePlayer.marker.setPopupContent(popupContent)
  448. }
  449. }
  450. // global.map.setView(point.slice(0, 2), 18); //地图中心跟踪最新位置
  451. }
  452. }, 500);
  453. storePlayer.interval_showTrail = interval_showTrail
  454. } else {
  455. // console.log('[drawOneTrail] 更新玩家轨迹', storePlayer, storePlayer.trail)
  456. }
  457. },
  458. getPlayerColor(playerId) {
  459. var colorIndex = playerId % 10
  460. return config.playerColorList[colorIndex]
  461. },
  462. // // duration: 轨迹保存时间 为0表示保存全部轨迹
  463. // getCurPos(lastpos, duration, player_trail) {
  464. // // console.log(lastpos, pointsT)
  465. // var now = +new Date();
  466. // if (duration > 0) {
  467. // player_trail.points = player_trail.points.filter(function(point) {
  468. // return now - point[2] < duration;
  469. // });
  470. // }
  471. // var step = 0.00001
  472. // var t = 10
  473. // var point = null
  474. // if (player_trail.pointsT < 20 * t)
  475. // point = [lastpos[0] + Math.random() * step, lastpos[1] + Math.random() * step, now];
  476. // else if (player_trail.pointsT >= 20 * t && player_trail.pointsT < 40 * t)
  477. // point = [lastpos[0] - Math.random() * step, lastpos[1] + Math.random() * step, now];
  478. // else if (player_trail.pointsT >= 40 * t && player_trail.pointsT < 60 * t)
  479. // point = [lastpos[0] - Math.random() * step, lastpos[1] - Math.random() * step, now];
  480. // else if (player_trail.pointsT >= 60 * t && player_trail.pointsT < 80 * t)
  481. // point = [lastpos[0] + Math.random() * step, lastpos[1] - Math.random() * step, now];
  482. // // point = [point[0] - Math.random() * 0.00001, point[1] - Math.random() * 0.00001, now];
  483. // player_trail.pointsT++
  484. // if (player_trail.pointsT >= 80 * t)
  485. // player_trail.pointsT = 0
  486. // player_trail.points.push(point);
  487. // return point
  488. // },
  489. free() {
  490. for (let i = 0; i < global.storePlayers.length; i++) {
  491. var storePlayer = global.storePlayers[i]
  492. if (storePlayer.interval_creatCircleMarker != null) {
  493. clearInterval(storePlayer.interval_creatCircleMarker)
  494. }
  495. if (storePlayer.interval_creatCircleMarker != null) {
  496. clearInterval(storePlayer.interval_showTrail)
  497. }
  498. }
  499. }
  500. }