player.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // import L from 'leaflet'
  2. import config from '@/utils/map/sub/config'
  3. import global from '@/utils/map/sub/global'
  4. var interval_creatCircleMarker = null
  5. var interval_showTrail = null
  6. var togglePlayerFlag = true
  7. var toggleTooltipFlag = true
  8. var toggleTrailFlag = true
  9. export default {
  10. playerLayerGroup: null, // 玩家标记图层组
  11. trailLayerGroup: null, // 玩家轨迹图层组
  12. init() {
  13. this.playerLayerGroup = new L.layerGroup()
  14. this.trailLayerGroup = new L.layerGroup()
  15. },
  16. // 即将开始地图缩放时触发本方法
  17. onZoomStart(e) {
  18. },
  19. // 地图发生缩放时触发本方法
  20. onZoom(e) {
  21. },
  22. // 地图缩放结束时触发本方法
  23. onZoomEnd(e) {
  24. },
  25. togglePlayer(flag) {
  26. if (flag != null) {
  27. togglePlayerFlag = !flag
  28. }
  29. if (togglePlayerFlag) {
  30. this.playerLayerGroup.removeFrom(global.map)
  31. togglePlayerFlag = false
  32. } else {
  33. this.playerLayerGroup.addTo(global.map)
  34. togglePlayerFlag = true
  35. }
  36. },
  37. toggleTooltip(flag) {
  38. if (flag != null) {
  39. toggleTooltipFlag = !flag
  40. }
  41. if (!togglePlayerFlag) {
  42. this.togglePlayer(true)
  43. toggleTooltipFlag = false
  44. }
  45. for (let i = 0; i < global.players.length; i++) {
  46. var player = global.getPlayerById(global.players[i].id)
  47. if (player.marker != null) {
  48. // player.marker.toggleTooltip()
  49. // let isopen = player.marker.isTooltipOpen()
  50. if (toggleTooltipFlag) {
  51. player.marker.closeTooltip()
  52. } else {
  53. player.marker.openTooltip()
  54. }
  55. }
  56. }
  57. toggleTooltipFlag = !toggleTooltipFlag
  58. },
  59. toggleTrail(flag) {
  60. if (flag != null) {
  61. toggleTrailFlag = !flag
  62. }
  63. if (toggleTrailFlag) {
  64. this.trailLayerGroup.removeFrom(global.map)
  65. toggleTrailFlag = false
  66. } else {
  67. this.trailLayerGroup.addTo(global.map)
  68. toggleTrailFlag = true
  69. }
  70. },
  71. drawAllPlayers() {
  72. console.log('[drawAllPlayers]', global.players)
  73. if (this.playerLayerGroup != null)
  74. this.playerLayerGroup.clearLayers()
  75. for (let i = 0; i < global.players.length; i++) {
  76. this.drawOnePlayer(global.players[i].id)
  77. // this.drawOnePlayer2(global.players[i].id)
  78. }
  79. },
  80. drawOnePlayer2(playerId, animate = true) {
  81. var that = this
  82. var player = global.getPlayerById(playerId)
  83. var player_position = global.getPlayerPositionById(playerId)
  84. // console.log(player, player_position)
  85. if (player == null || player_position == null) {
  86. console.warn('[drawOnePlayer2] 关键数据为空', player, player_position)
  87. return
  88. }
  89. // 在地图上创建 marker 并存储用户信息
  90. var playerIcon = L.icon({
  91. iconUrl: 'static/image/marker-icon.png',
  92. iconSize: [20, 32.8],
  93. iconAnchor: [10, 30]
  94. });
  95. var marker = L.marker([player_position.latitude, player_position.longitude], {
  96. icon: playerIcon
  97. })
  98. .addTo(this.playerLayerGroup)
  99. // .addTo(global.map)
  100. // .bindPopup("Hello, I'm a Marker!<br><img src='my-image.png' width='100'>").openPopup();
  101. .bindTooltip(`${player.name}`, {
  102. permanent: true,
  103. offset: [0, -30],
  104. direction: 'top',
  105. interactive: true
  106. })
  107. marker.type = 'marker'
  108. marker.playerId = player.id // 存储用户信息
  109. marker.animate = animate
  110. marker.on('click', function(e) {
  111. var playerId = e.target.playerId; // 获取点击的 marker 的用户信息
  112. console.log("Selected player ID: " + playerId);
  113. that.handleFocusPlayer(playerId)
  114. });
  115. var tooltip = marker.getTooltip()
  116. tooltip.playerId = player.id // 存储用户信息
  117. tooltip.animate = animate
  118. tooltip.on('click', function(e) {
  119. // console.log("[Marker.Tooltip]", e)
  120. var playerId = e.target.playerId; // 获取点击的 marker 的用户信息
  121. console.log("[Marker.Tooltip] Selected player ID: " + playerId)
  122. that.handleFocusPlayer(playerId)
  123. })
  124. // this.playerLayerGroup.addTo(global.map)
  125. player.marker = marker
  126. },
  127. // 创建动画效果函数,用于实现circleMarker的大小和透明度随时间变化
  128. animateCircle(circle, minRadius, maxRadius, step = 1) {
  129. var radius = circle.getRadius();
  130. var opacity = circle.options.fillOpacity;
  131. var zoomType = 'zoomIn' // zoomIn: 放大 zoomOut: 缩小
  132. interval_creatCircleMarker = setInterval(function() {
  133. if (!circle.animate) {
  134. clearInterval(interval_creatCircleMarker)
  135. return
  136. }
  137. // 改变圆形半径和透明度
  138. if (zoomType == 'zoomIn') {
  139. radius = radius + step
  140. // opacity = opacity - 0.1
  141. } else if (zoomType == 'zoomOut') {
  142. radius = radius - step
  143. // opacity = opacity - 0.1
  144. }
  145. circle.setRadius(radius);
  146. circle.setStyle({
  147. fillOpacity: opacity
  148. });
  149. if (radius >= maxRadius) {
  150. zoomType = 'zoomOut'
  151. } else if (radius <= minRadius) {
  152. zoomType = 'zoomIn'
  153. }
  154. }, 50);
  155. },
  156. drawOnePlayer(playerId, animate = true) {
  157. var that = this
  158. var player = global.getPlayerById(playerId)
  159. var player_position = global.getPlayerPositionById(playerId)
  160. console.log('[drawOnePlayer]', player, player_position)
  161. if (player == null || player_position == null) {
  162. console.warn('[drawOnePlayer] 关键数据为空', player, player_position)
  163. return
  164. }
  165. var marker = L.circleMarker([player_position.latitude, player_position.longitude], config.gStyle.marker.default)
  166. .addTo(this.playerLayerGroup)
  167. // .addTo(global.map)
  168. // .bindPopup("Hello, I'm a Marker!<br><img src='my-image.png' width='100'>").openPopup()
  169. .bindTooltip(`${player.name}`, config.gStyle.marker.tooltip)
  170. marker.type = 'circleMarker'
  171. marker.playerId = player.id // 存储用户信息
  172. marker.animate = animate
  173. marker.on('click', function(e) {
  174. // console.log("[Marker]", e)
  175. var playerId = e.target.playerId; // 获取点击的 marker 的用户信息
  176. console.log("[Marker] Selected player ID: " + playerId)
  177. that.handleFocusPlayer(playerId)
  178. })
  179. var tooltip = marker.getTooltip()
  180. tooltip.playerId = player.id // 存储用户信息
  181. tooltip.animate = animate
  182. tooltip.on('click', function(e) {
  183. // console.log("[Marker.Tooltip]", e)
  184. var playerId = e.target.playerId; // 获取点击的 marker 的用户信息
  185. console.log("[Marker.Tooltip] Selected player ID: " + playerId)
  186. that.handleFocusPlayer(playerId)
  187. })
  188. if (animate) {
  189. this.animateCircle(marker, 3, 9, 0.5)
  190. }
  191. // this.playerLayerGroup.addTo(global.map)
  192. player.marker = marker
  193. },
  194. handleFocusPlayer(playerId) {
  195. console.log("[handleFocusPlayer] 当前选中玩家ID: " + playerId)
  196. if (global.focusPlayerId > 0) {
  197. // 先把之前选中的目标恢复成默认状态
  198. var unfocusPlayer = global.getPlayerById(global.focusPlayerId)
  199. if (unfocusPlayer.marker != null && unfocusPlayer.marker.type == 'circleMarker') {
  200. unfocusPlayer.marker.setStyle(config.gStyle.marker.default)
  201. }
  202. if (unfocusPlayer.trail != null) {
  203. unfocusPlayer.trail.setStyle(config.gStyle.trail.default)
  204. }
  205. }
  206. global.focusPlayerId = playerId
  207. var focusPlayer = global.getPlayerById(global.focusPlayerId)
  208. if (focusPlayer.marker != null && focusPlayer.marker.type == 'circleMarker') {
  209. focusPlayer.marker.setStyle(config.gStyle.marker.focus)
  210. }
  211. if (focusPlayer.trail != null) {
  212. focusPlayer.trail.setStyle(config.gStyle.trail.focus)
  213. }
  214. },
  215. drawAllTrails(duration) {
  216. if (this.trailLayerGroup != null)
  217. this.trailLayerGroup.clearLayers()
  218. for (let i = 0; i < global.players.length; i++) {
  219. this.drawOneTrail(global.players[i].id, duration)
  220. }
  221. },
  222. // 显示运动轨迹 duration:毫秒
  223. drawOneTrail(playerId, duration, animate = false) {
  224. var player = global.getPlayerById(playerId)
  225. var player_position = global.getPlayerPositionById(playerId)
  226. var player_trail = global.getPlayerTrailById(playerId)
  227. // console.log('[drawOneTrail] param', player, player_position, player_trail)
  228. if (player == null || player_position == null) {
  229. console.warn('[drawOneTrail] 关键数据为空', player, player_position)
  230. return
  231. }
  232. var trail = player.trail
  233. var curPoint = [player_position.latitude, player_position.longitude, new Date()]
  234. console.log('[drawOneTrail] curPoint', curPoint)
  235. if (player_trail == null) {
  236. // if (player.trail == null) {
  237. console.warn('[drawOneTrail] 轨迹数据为空', player, player.trail, player_trail)
  238. // return
  239. var points = curPoint
  240. // var points = curPoint.slice(0, 2)
  241. player_trail = {
  242. id: playerId,
  243. points: [
  244. points
  245. ],
  246. pointsT: 0,
  247. // trail: trail
  248. }
  249. global.players_trail.push(player_trail)
  250. trail = L.polyline(player_trail.points, config.gStyle.trail.default)
  251. .addTo(this.trailLayerGroup)
  252. // .addTo(global.map)
  253. console.log('[drawOneTrail] trail', trail)
  254. player.trail = trail
  255. }
  256. else {
  257. // 去除过期的历史轨迹
  258. // if (duration > 0) {
  259. // var now = +new Date();
  260. // player_trail.points = player_trail.points.filter(function(point) {
  261. // return now - point[2] < duration;
  262. // });
  263. // }
  264. // player_trail.points.push(curPoint)
  265. // player_trail.points.push(curPoint.slice(0, 2))
  266. }
  267. // console.log('[drawOneTrail] player_trail.points', player, player_trail.points)
  268. console.log('[drawOneTrail] player_trail.points', JSON.stringify(player_trail.points))
  269. // trail.setLatLngs(player_trail.points)
  270. // trail.addLatLng(curPoint.slice(0, 2))
  271. // if (player.marker != null) {
  272. // player.marker.setLatLng(curPoint.slice(0, 2)) // 更新玩家的标记位置
  273. // }
  274. // if (player.marker != null) {
  275. // player.marker.animate = animate
  276. // }
  277. // var lastpos = [player_position.latitude, player_position.longitude, new Date()]
  278. // var point = null
  279. var that = this
  280. interval_showTrail = setInterval(function() {
  281. // 去除过期的历史轨迹
  282. if (duration > 0) {
  283. var now = +new Date();
  284. player_trail.points = player_trail.points.filter(function(point) {
  285. return now - point[2] < duration;
  286. });
  287. }
  288. player_trail.points.push(curPoint)
  289. player_position = global.getPlayerPositionById(playerId)
  290. curPoint = [player_position.latitude, player_position.longitude, new Date()]
  291. // player_trail.points.push(curPoint.slice(0, 2))
  292. // console.log('interval_showTrail', player_trail.points)
  293. trail.setLatLngs(player_trail.points);
  294. // trail.addLatLng(point.slice(0, 2))
  295. if (player.marker != null) {
  296. player.marker.setLatLng(curPoint.slice(0, 2)) // 更新玩家的标记位置
  297. }
  298. // global.map.setView(point.slice(0, 2), 18); //地图中心跟踪最新位置
  299. }, 100);
  300. // this.trailLayerGroup.addTo(global.map)
  301. },
  302. // duration: 轨迹保存时间 为0表示保存全部轨迹
  303. getCurPos(lastpos, duration, player_trail) {
  304. // console.log(lastpos, pointsT)
  305. var now = +new Date();
  306. if (duration > 0) {
  307. player_trail.points = player_trail.points.filter(function(point) {
  308. return now - point[2] < duration;
  309. });
  310. }
  311. var step = 0.00001
  312. var t = 10
  313. var point = null
  314. if (player_trail.pointsT < 20*t)
  315. point = [lastpos[0] + Math.random() * step, lastpos[1] + Math.random() * step, now];
  316. else if (player_trail.pointsT >= 20*t && player_trail.pointsT < 40*t)
  317. point = [lastpos[0] - Math.random() * step, lastpos[1] + Math.random() * step, now];
  318. else if (player_trail.pointsT >= 40*t && player_trail.pointsT < 60*t)
  319. point = [lastpos[0] - Math.random() * step, lastpos[1] - Math.random() * step, now];
  320. else if (player_trail.pointsT >= 60*t && player_trail.pointsT < 80*t)
  321. point = [lastpos[0] + Math.random() * step, lastpos[1] - Math.random() * step, now];
  322. // point = [point[0] - Math.random() * 0.00001, point[1] - Math.random() * 0.00001, now];
  323. player_trail.pointsT++
  324. if (player_trail.pointsT >= 80*t)
  325. player_trail.pointsT = 0
  326. player_trail.points.push(point);
  327. return point
  328. },
  329. free() {
  330. clearInterval(interval_creatCircleMarker)
  331. clearInterval(interval_showTrail)
  332. }
  333. }