shipHisMap.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. var _this, dataObj = [], baseTok = null;
  2. var map;
  3. var pathSimplifierIns;
  4. var pathSimplifierInsta = false;
  5. var navg0 = null, lineArr = [], routeInfo= [];
  6. new Vue({
  7. el: '#app',
  8. data: function() {
  9. return {
  10. shipLi: [],
  11. nowTab: {},
  12. userForm: {},
  13. queryParams: {
  14. key: "643a63b2f21104345c47104702061afe",
  15. mmsi: "",
  16. start: "",
  17. end: ""
  18. },
  19. startDay: "",
  20. endDay: "",
  21. lengSli: 0,
  22. dataMax: 10,
  23. speedSli: 5,
  24. animating: 0,
  25. nowHis: {}
  26. }
  27. },
  28. created() {
  29. _this = this;
  30. baseTok = getCookie("Admin-Token") != "null" && getCookie("Admin-Token") != null ? getCookie("Admin-Token") : "";
  31. _this.userForm = JSON.parse(getCookie("Admin-User"));
  32. _this.startDay = parseTime(new Date(), '{y}-{m}-{d}') + ' 00:00:00';
  33. _this.endDay = parseTime(new Date(), '{y}-{m}-{d} {h}:{d}:{s}');
  34. _this.initAjax();
  35. },
  36. mounted(){
  37. map = new AMap.Map("areaMap", {
  38. center: [113.5556, 22.9422],
  39. zoom: 14
  40. });
  41. window.addEventListener('message',(data)=>{
  42. if(data.data != _this.nowTab.sn){
  43. _this.changTab(data.data);
  44. }
  45. })
  46. },
  47. methods: {
  48. initAjax(){
  49. const loading = this.$loading({ background: 'rgba(0, 0, 0, 0.4)' }) let _opts = {
  50. url: baseUrl + "/SelCctvListByUserId",
  51. type:"GET"
  52. };
  53. let _data = {
  54. "user_id": _this.userForm.userId
  55. };
  56. let _hea = {
  57. "Authorization": "Bearer " + baseTok
  58. }; getAxios(_opts,_data,_hea).then((res) => { if(res.code == 200&&res.data.length){
  59. _this.shipLi = res.data;
  60. let _sn = JSON.parse(localStorage.getItem("nowShip"));
  61. if(_sn !=null && _sn != "null"){
  62. let _ind = _this.shipLi.findIndex((i,ind) => {return i.sn == _sn.sn});
  63. if(_ind != -1){
  64. _sn = _this.shipLi[_ind];
  65. }else{
  66. _sn = _this.shipLi[0];
  67. }
  68. }else{
  69. _sn = _this.shipLi[0];
  70. }
  71. _this.nowTab = _sn;
  72. }else{ _this.shipLi = []; } loading.close(); }) },
  73. changTab(id){
  74. let ind = _this.shipLi.findIndex((it,ind) => {return id == it.sn});
  75. if(ind != -1){
  76. _this.nowTab = _this.shipLi[ind];
  77. _this.getAjaxHisMap();
  78. window.parent.postMessage({ id: _this.nowTab.sn },staticUrl+ "/");
  79. }
  80. },
  81. getAjaxHisMap(){
  82. if(isNaN(Date.parse(_this.endDay))||isNaN(Date.parse(_this.startDay))){
  83. _this.$message({
  84. message: '请选择时间!',
  85. type: 'warning'
  86. });
  87. return;
  88. }
  89. if(_this.nowTab.mmsi != ""){
  90. _this.queryParams.mmsi = _this.nowTab.mmsi;
  91. _this.queryParams.start = new Date(_this.startDay).getTime();
  92. _this.queryParams.end = new Date(_this.endDay).getTime();
  93. getHisMap(_this.queryParams, function(_res){
  94. lineArr = [];
  95. routeInfo = [];
  96. _this.nowHis = {}
  97. if(_res[_this.queryParams.mmsi].length){
  98. let _obj = _res[_this.queryParams.mmsi];
  99. _obj.forEach(item => {
  100. item["stime"] = parseTime(new Date(parseInt(item.createdAt)), '{y}-{m}-{d} {h}:{d}:{s}');
  101. let _newpo = GpsToGd(Number(item.lat), Number(item.lon));
  102. lineArr.push([_newpo.lon,_newpo.lat]);
  103. })
  104. routeInfo = _obj;
  105. _this.renderTrace(lineArr, 5000);
  106. }else{
  107. if (pathSimplifierInsta){
  108. pathSimplifierIns.setData([]);
  109. }
  110. _this.$message({
  111. message: '该时间段暂无历史轨迹!',
  112. type: 'warning'
  113. });
  114. return;
  115. };
  116. })
  117. }
  118. },
  119. renderTrace(Path,speed) {
  120. //判断是否已有轨迹,进行清除!!!注意,就是在此处清除了轨迹
  121. if (pathSimplifierInsta) {
  122. pathSimplifierIns.setData([]);
  123. }
  124. AMapUI.load(['ui/misc/PathSimplifier', 'lib/$'], function(PathSimplifier, $) {
  125. if (!PathSimplifier.supportCanvas) {
  126. alert('当前环境不支持 Canvas!');
  127. return;
  128. }
  129. _this.dataMax = lineArr.length;
  130. map.setFeatures(['bg', 'point', 'building']);
  131. map.setFitView();
  132. map.setCenter(lineArr[0]);
  133. pathSimplifierIns = new PathSimplifier({
  134. zIndex: 100,
  135. //autoSetFitView:false,
  136. map: map, //所属的地图实例
  137. getPath: function(pathData, pathIndex) {
  138. return pathData.path;
  139. },
  140. getHoverTitle: function(pathData, pathIndex, pointIndex) {
  141. if (pointIndex >= 0) {
  142. return pathData.path[pointIndex][0]+ ',' + pathData.path[pointIndex][1];
  143. }
  144. return pathData.name + '历史轨迹';
  145. },
  146. renderOptions: {
  147. renderAllPointsIfNumberBelow: 100 //绘制路线节点,如不需要可设置为-1
  148. }
  149. });
  150. pathSimplifierInsta = true;
  151. //设置数据
  152. pathSimplifierIns.setData([{
  153. name: _this.nowTab.dev_name,
  154. path: Path //传入的轨迹数据
  155. }]);
  156. //对第一条线路(即索引 0)创建一个巡航器
  157. navg0 = pathSimplifierIns.createPathNavigator(0, {
  158. loop: false, //循环播放
  159. speed: _this.speedSli*1000 //巡航速度,单位千米/小时
  160. });
  161. navg0.on("move", function(data,position) {
  162. let idx = position.dataItem.pointIndex;
  163. _this.lengSli = idx;
  164. _this.nowHis = routeInfo[idx];
  165. map.setCenter(navg0.getPosition());
  166. if (navg0.isCursorAtPathEnd()) {
  167. _this.animating = 0;
  168. }
  169. })
  170. map.setFitView(); //根据地图上添加的覆盖物分布情况,自动缩放地图到合适的视野级别,参数overlayList默认为当前地图上添加的所有覆盖物图层
  171. });
  172. },
  173. startMove(){
  174. if(navg0 == null){
  175. _this.$message({
  176. message: '该时间段暂无历史轨迹!',
  177. type: 'warning'
  178. });
  179. return;
  180. }
  181. _this.lengSli = 0;
  182. _this.animating = 2;
  183. navg0.start();
  184. },
  185. stopMove(){
  186. navg0.pause();
  187. _this.animating = 1;
  188. },
  189. continueMove(){
  190. navg0.resume();
  191. _this.animating = 2;
  192. },
  193. changeSpeed(e){
  194. navg0.setSpeed(e*1000);
  195. }
  196. }
  197. })