player.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. if (this.playerLayerGroup != null)
  73. this.playerLayerGroup.clearLayers()
  74. for (let i = 0; i < global.players.length; i++) {
  75. this.drawOnePlayer(global.players[i].id)
  76. // this.drawOnePlayer2(global.players[i].id)
  77. }
  78. },
  79. drawOnePlayer2(playerId, animate = true) {
  80. var that = this
  81. var player = global.getPlayerById(playerId)
  82. var player_position = global.getPlayerPositionById(playerId)
  83. // console.log(player, player_position)
  84. if (player == null || player_position == null) {
  85. console.warn('[drawOnePlayer2] 关键数据为空', player, player_position)
  86. return
  87. }
  88. // 在地图上创建 marker 并存储用户信息
  89. var playerIcon = L.icon({
  90. iconUrl: 'static/image/marker-icon.png',
  91. iconSize: [20, 32.8],
  92. iconAnchor: [10, 30]
  93. });
  94. var marker = L.marker([player_position.lat, player_position.lng], {
  95. icon: playerIcon
  96. })
  97. .addTo(this.playerLayerGroup)
  98. // .addTo(global.map)
  99. // .bindPopup("Hello, I'm a Marker!<br><img src='my-image.png' width='100'>").openPopup();
  100. .bindTooltip(`${player.nickName} ${player.phone}`, {
  101. permanent: true,
  102. offset: [0, -30],
  103. direction: 'top',
  104. interactive: true
  105. })
  106. marker.type = 'marker'
  107. marker.playerId = player.id // 存储用户信息
  108. marker.animate = animate
  109. marker.on('click', function(e) {
  110. var playerId = e.target.playerId; // 获取点击的 marker 的用户信息
  111. console.log("Selected player ID: " + playerId);
  112. that.handleFocusPlayer(playerId)
  113. });
  114. var tooltip = marker.getTooltip()
  115. tooltip.playerId = player.id // 存储用户信息
  116. tooltip.animate = animate
  117. tooltip.on('click', function(e) {
  118. // console.log("[Marker.Tooltip]", e)
  119. var playerId = e.target.playerId; // 获取点击的 marker 的用户信息
  120. console.log("[Marker.Tooltip] Selected player ID: " + playerId)
  121. that.handleFocusPlayer(playerId)
  122. })
  123. // this.playerLayerGroup.addTo(global.map)
  124. player.marker = marker
  125. },
  126. // 创建动画效果函数,用于实现circleMarker的大小和透明度随时间变化
  127. animateCircle(circle, minRadius, maxRadius, step = 1) {
  128. var radius = circle.getRadius();
  129. var opacity = circle.options.fillOpacity;
  130. var zoomType = 'zoomIn' // zoomIn: 放大 zoomOut: 缩小
  131. interval_creatCircleMarker = setInterval(function() {
  132. if (!circle.animate) {
  133. clearInterval(interval_creatCircleMarker)
  134. return
  135. }
  136. // 改变圆形半径和透明度
  137. if (zoomType == 'zoomIn') {
  138. radius = radius + step
  139. // opacity = opacity - 0.1
  140. } else if (zoomType == 'zoomOut') {
  141. radius = radius - step
  142. // opacity = opacity - 0.1
  143. }
  144. circle.setRadius(radius);
  145. circle.setStyle({
  146. fillOpacity: opacity
  147. });
  148. if (radius >= maxRadius) {
  149. zoomType = 'zoomOut'
  150. } else if (radius <= minRadius) {
  151. zoomType = 'zoomIn'
  152. }
  153. }, 50);
  154. },
  155. drawOnePlayer(playerId, animate = true) {
  156. var that = this
  157. var player = global.getPlayerById(playerId)
  158. var player_position = global.getPlayerPositionById(playerId)
  159. // console.log(player, player_position)
  160. if (player == null || player_position == null) {
  161. console.warn('[drawOnePlayer] 关键数据为空', player, player_position)
  162. return
  163. }
  164. var marker = L.circleMarker([player_position.lat, player_position.lng], config.gStyle.marker.default)
  165. .addTo(this.playerLayerGroup)
  166. // .addTo(global.map)
  167. // .bindPopup("Hello, I'm a Marker!<br><img src='my-image.png' width='100'>").openPopup()
  168. .bindTooltip(`${player.nickName} ${player.phone}`, config.gStyle.marker.tooltip)
  169. marker.type = 'circleMarker'
  170. marker.playerId = player.id // 存储用户信息
  171. marker.animate = animate
  172. marker.on('click', function(e) {
  173. // console.log("[Marker]", e)
  174. var playerId = e.target.playerId; // 获取点击的 marker 的用户信息
  175. console.log("[Marker] Selected player ID: " + playerId)
  176. that.handleFocusPlayer(playerId)
  177. })
  178. var tooltip = marker.getTooltip()
  179. tooltip.playerId = player.id // 存储用户信息
  180. tooltip.animate = animate
  181. tooltip.on('click', function(e) {
  182. // console.log("[Marker.Tooltip]", e)
  183. var playerId = e.target.playerId; // 获取点击的 marker 的用户信息
  184. console.log("[Marker.Tooltip] Selected player ID: " + playerId)
  185. that.handleFocusPlayer(playerId)
  186. })
  187. if (animate) {
  188. this.animateCircle(marker, 3, 9, 0.5)
  189. }
  190. // this.playerLayerGroup.addTo(global.map)
  191. player.marker = marker
  192. },
  193. handleFocusPlayer(playerId) {
  194. console.log("[handleFocusPlayer] 当前选中玩家ID: " + playerId)
  195. if (global.focusPlayerId > 0) {
  196. // 先把之前选中的目标恢复成默认状态
  197. var unfocusPlayer = global.getPlayerById(global.focusPlayerId)
  198. if (unfocusPlayer.marker != null && unfocusPlayer.marker.type == 'circleMarker') {
  199. unfocusPlayer.marker.setStyle(config.gStyle.marker.default)
  200. }
  201. if (unfocusPlayer.trail != null) {
  202. unfocusPlayer.trail.setStyle(config.gStyle.trail.default)
  203. }
  204. }
  205. global.focusPlayerId = playerId
  206. var focusPlayer = global.getPlayerById(global.focusPlayerId)
  207. if (focusPlayer.marker != null && focusPlayer.marker.type == 'circleMarker') {
  208. focusPlayer.marker.setStyle(config.gStyle.marker.focus)
  209. }
  210. if (focusPlayer.trail != null) {
  211. focusPlayer.trail.setStyle(config.gStyle.trail.focus)
  212. }
  213. },
  214. drawAllTrails(duration) {
  215. if (this.trailLayerGroup != null)
  216. this.trailLayerGroup.clearLayers()
  217. for (let i = 0; i < global.players.length; i++) {
  218. this.drawOneTrail(global.players[i].id, duration)
  219. }
  220. },
  221. // 显示运动轨迹 duration:毫秒
  222. drawOneTrail(playerId, duration, animate = false) {
  223. var player = global.getPlayerById(playerId)
  224. var player_position = global.getPlayerPositionById(playerId)
  225. var player_trail = global.getPlayerTrailById(playerId)
  226. // console.log(player, player_position, player_trail)
  227. if (player == null || player_position == null) {
  228. console.warn('[drawOneTrail] 关键数据为空', player, player_position)
  229. return
  230. }
  231. if (player_trail == null) {
  232. // console.warn('[drawOneTrail] 轨迹数据为空', player_trail)
  233. // return
  234. player_trail = {
  235. id: playerId,
  236. points: [
  237. [player_position.lat, player_position.lng, new Date()]
  238. ],
  239. pointsT: 0
  240. }
  241. global.players_trail.push(player_trail)
  242. }
  243. if (player.marker != null) {
  244. player.marker.animate = animate
  245. }
  246. var trail = L.polyline(player_trail.points, config.gStyle.trail.default)
  247. .addTo(this.trailLayerGroup)
  248. // .addTo(global.map)
  249. var lastpos = [player_position.lat, player_position.lng, new Date()]
  250. var point = null
  251. var that = this
  252. interval_showTrail = setInterval(function() {
  253. trail.setLatLngs(player_trail.points);
  254. point = that.getCurPos(lastpos, duration, player_trail)
  255. lastpos = point
  256. trail.addLatLng(point.slice(0, 2))
  257. if (player.marker != null) {
  258. player.marker.setLatLng(point.slice(0, 2)) // 更新玩家的标记位置
  259. }
  260. // global.map.setView(point.slice(0, 2), 18); //地图中心跟踪最新位置
  261. }, 100);
  262. // this.trailLayerGroup.addTo(global.map)
  263. player.trail = trail
  264. },
  265. // duration: 轨迹保存时间 为0表示保存全部轨迹
  266. getCurPos(lastpos, duration, player_trail) {
  267. // console.log(lastpos, pointsT)
  268. var now = +new Date();
  269. if (duration > 0) {
  270. player_trail.points = player_trail.points.filter(function(point) {
  271. return now - point[2] < duration;
  272. });
  273. }
  274. var step = 0.00001
  275. var t = 10
  276. var point = null
  277. if (player_trail.pointsT < 20*t)
  278. point = [lastpos[0] + Math.random() * step, lastpos[1] + Math.random() * step, now];
  279. else if (player_trail.pointsT >= 20*t && player_trail.pointsT < 40*t)
  280. point = [lastpos[0] - Math.random() * step, lastpos[1] + Math.random() * step, now];
  281. else if (player_trail.pointsT >= 40*t && player_trail.pointsT < 60*t)
  282. point = [lastpos[0] - Math.random() * step, lastpos[1] - Math.random() * step, now];
  283. else if (player_trail.pointsT >= 60*t && player_trail.pointsT < 80*t)
  284. point = [lastpos[0] + Math.random() * step, lastpos[1] - Math.random() * step, now];
  285. // point = [point[0] - Math.random() * 0.00001, point[1] - Math.random() * 0.00001, now];
  286. player_trail.pointsT++
  287. if (player_trail.pointsT >= 80*t)
  288. player_trail.pointsT = 0
  289. player_trail.points.push(point);
  290. return point
  291. },
  292. free() {
  293. clearInterval(interval_creatCircleMarker)
  294. clearInterval(interval_showTrail)
  295. }
  296. }