HistoryTrack.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import React, { Component } from 'react';
  2. import PubSub from 'pubsub-js';
  3. import { withLeaflet } from 'react-leaflet';
  4. import L, { LatLng } from 'leaflet';
  5. import 'leaflet-trackplayer';
  6. /**
  7. * HistoryTrack layer allows to show
  8. */
  9. class HistoryTrack extends Component {
  10. static HIS_PLAY = 0;
  11. static HIS_PAUSE = 1;
  12. static HIS_CLEAR = 2;
  13. static HIS_SET_SPEED = 3;
  14. static HIS_SET_PROGRESS = 4;
  15. path = [];
  16. cur_index = 0;
  17. cur_speed = 1;
  18. cur_multi = 1;
  19. componentDidMount(){
  20. this.map = this.props.map.leafletElement;
  21. // console.log(this.map, this.props);
  22. PubSub.subscribe("his.play.ctrl", (msg, data) => {
  23. // console.log(data.state);
  24. if(!this.track){
  25. console.error("需要先设置track数据!")
  26. alert("需要先设置track数据!");
  27. return;
  28. }
  29. switch (data.state) {
  30. case HistoryTrack.HIS_PLAY:
  31. this.track.start();
  32. break;
  33. case HistoryTrack.HIS_PAUSE:
  34. this.track.pause();
  35. break;
  36. case HistoryTrack.HIS_CLEAR:
  37. this.track.remove();
  38. break;
  39. case HistoryTrack.HIS_SET_SPEED:
  40. this.cur_multi = data.speedMultiply;
  41. this.track.setSpeed(this.cur_multi * this.cur_speed);
  42. break;
  43. case HistoryTrack.HIS_SET_PROGRESS:
  44. this.track.setProgress(data.progress);
  45. break;
  46. default:
  47. console.error("HistoryTrack 未知状态!")
  48. break;
  49. }
  50. });
  51. PubSub.subscribe("map.his.set", (msg, data) => {
  52. this.cur_index = 0;
  53. this.cur_distance = 0;
  54. this.cur_speed = 1;
  55. // console.log('set his');
  56. this.path = data.path;
  57. if(this.track){
  58. this.track.remove();
  59. }
  60. // console.log(data.src);
  61. let hisIcon = L.icon({
  62. iconSize: [20, 20],
  63. iconUrl: data.src,
  64. iconAnchor: [10, 10]
  65. });
  66. this.cur_multi = data.speedMultiply;
  67. this.cur_speed = this.getSpeed(this.cur_index);
  68. this.track = new L.TrackPlayer(this.path, {
  69. markerIcon: hisIcon,
  70. speed: this.cur_multi * this.cur_speed,
  71. weight: 10,
  72. passedLineColor: '#32CD32',
  73. notPassedLineColor: '#00BFFF',
  74. panTo: false,
  75. markerRotate: true,
  76. });
  77. this.track.addTo(this.map);
  78. this.track.on('progress', this.deal);
  79. });
  80. }
  81. deal = (a, b, c) => {
  82. if(this.cur_index !== c){
  83. this.cur_index = c;
  84. this.cur_speed = this.getSpeed(this.cur_index);
  85. this.track.setSpeed(this.cur_multi * this.cur_speed);
  86. // console.log(c, "speed ", this.cur_speed, "千米/h");
  87. }
  88. }
  89. getSpeed(index){
  90. if(index + 1 === this.path.length) return 0;
  91. let ll1 = new LatLng(this.path[index].lat, this.path[index].lng);
  92. let ll2 = new LatLng(this.path[index + 1].lat, this.path[index + 1].lng);
  93. let distance = ll1.distanceTo(ll2) / 1000;
  94. let speed = distance / (this.path[index + 1].time - this.path[index].time) * 3600000;
  95. // console.log((this.path[index + 1].time - this.path[index].time));
  96. return speed;
  97. }
  98. componentWillUnmount(){
  99. PubSub.unsubscribe("map.his.set");
  100. PubSub.unsubscribe("map.play.ctrl");
  101. }
  102. render(){
  103. return <>
  104. </>;
  105. }
  106. }
  107. export default withLeaflet(HistoryTrack);