replay.vue
19.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
<template>
<div class="info-content">
<div class="tab">
<div class="tabbar">
<div class="tabbar-item" @click="showTimeDialog = true">
轨迹时间
<span class="slect-class">{{ currDateText }}</span>
<van-icon class="icon-rotate" name="play" />
</div>
<div class="tabbar-item" @click="changeSpeedPickerPopup">
回放速度
<span class="slect-class">{{ selectSpeedId || "" }}</span>
<van-icon class="icon-rotate" name="play" />
</div>
</div>
</div>
<!-- 回放速度 -->
<popup
:show="showSpeedPicker"
@btnCancel="showSpeedPicker = false"
@btnSave="saveSpeed"
:position="'bottom'"
>
<div class="speedSelect-box">
<div class="box">
<vue-slider
v-model="speedId"
:marks="true"
:data="speedItemText"
:interval="1"
:max="3"
:height="2"
:dotSize="16"
tooltip="none"
@change="changSpeed"
>
<template v-slot:step="{ label, active }">
<div :class="['custom-step', { label, active }]"></div>
</template>
</vue-slider>
</div>
</div>
</popup>
<!-- 轨迹时间 -->
<popup
:show="showTimeDialog"
@btnCancel="showTimeDialog = false"
@btnSave="saveTime"
:position="'bottom'"
>
<div class="time-content">
<div class="time-btn-list">
<span class="label">回放时段</span>
<span
class="time_btn"
v-for="(item, index) in timeBtnList"
:key="index"
:class="timeSelected === index ? 'active' : ''"
@click="selectTime(index)"
>{{ item }}</span
>
</div>
<div class="custom-time">
<span class="label">自定义时间</span>
<div @click="onShowTimePicker">
<span>{{ customTime }}</span>
<van-icon name="arrow" />
</div>
</div>
<van-datetime-picker
style="width: 100%"
v-if="showTimePicker"
v-model="currentDate"
:max-date="maxDate"
type="date"
@confirm="changeCurrTiem"
ref="dateTimePicker"
/>
</div>
</popup>
<mapContent
@refreshCb="refreshCb"
@getData="getCarReplayData"
ref="mapContentRefs"
>
<template #mapBox>
<div id="replayContainer" class="map-container"></div>
</template>
</mapContent>
<div class="btn-control">
<a class="icon icon-replay-play" v-if="!isPlay" @click="play"></a>
<a class="icon icon-replay-pause" v-else @click="pause"></a>
<a class="icon icon-replay-refresh" @click="replayPath"></a>
</div>
</div>
</template>
<script>
import { defineComponent, onMounted, ref, reactive, nextTick } from "vue";
import mapContent from "@/components/mapContent.vue";
import popup from "@/components/popup.vue";
// import AMapLoader from "@amap/amap-jsapi-loader";
import { CONSTANT } from "../constant";
import { Toast, Dialog } from "vant";
import moment from "@/utils/moment";
import "vant/es/toast/style";
import "vant/es/dialog/style";
import { GET_REPLAY_DATA } from "@/api/index";
import carPng from "@/assets/images/car.png";
import { useRoute } from "vue-router";
import VueSlider from "vue-slider-component";
import "vue-slider-component/theme/default.css";
export default defineComponent({
name: "ReplayPage",
components: {
mapContent,
["vue-slider"]: VueSlider,
popup,
},
setup() {
const mapContentRefs = ref(null);
const showSpeedPicker = ref(false);
const speedId = ref("正常");
const selectSpeedId = ref("正常");
const eParams = ref({});
const timeSelected = ref(0);
const showTimeDialog = ref(false);
const showTimePicker = ref(false);
const currentDate = ref(new Date());
const timeBtnList = ["24小时", "3天", "7天"];
const customTime = ref("");
var idx = ref(0)
const searchTime = ref([
moment().subtract(1, "day").format("YYYY-MM-DD HH:mm:ss"),
moment().format("YYYY-MM-DD HH:mm:ss"),
]);
const route = useRoute();
const currDateText = ref("24小时");
const maxDate = ref(new Date());
const replayList = ref([]);
const dateTimePicker = ref(null);
let currentMap = null;
let routeQuery = route.query;
const replay = reactive({
PathS: null, //轨迹构造器
pathSimplifierIns: null, //轨迹实例
navg: null, //轨迹巡航器
dis: null, // 轨迹距离
speed: 0,
});
const isPlay = ref(false);
const speedItemText = ["慢", "正常", "快", "很快"];
onMounted(() => {
mapContentRefs.value.initMap("replayContainer", 1);
// AMapLoader.load({
// key: CONSTANT.MAP_KEY,
// version: "1.4.8",
// plugins: CONSTANT.MAP_PLUGINS,
// AMapUI: {
// version: "1.1",
// plugins: ["overlay/SimpleMarker"],
// },
// }).then((AMap) => {
// window.AMap = AMap;
// mapContentRefs.value.initMap("replayContainer");
// });
});
const refreshCb = () => {
// if (replay.navg) {
// replay.navg.destroy();
// }
if (replay.pathSimplifierIns) {
replay.pathSimplifierIns.setData({
path: [],
});
}
isPlay.value = false;
window.mapapi.invoke("clearOverlays");
getCarReplayData();
};
// 获取车辆轨迹
const getCarReplayData = (map) => {
currentMap = map ? map : currentMap;
getParams();
// if (replay.navg) {
// replay.navg.destroy();
// }
if (replay.pathSimplifierIns) {
replay.pathSimplifierIns.setData({
path: [],
});
}
isPlay.value = false;
currentMap && window.mapapi.invoke("clearOverlays");
GET_REPLAY_DATA({ ...eParams.value }).then((res) => {
showTimeDialog.value = false;
if (res.code === 200) {
if (res.data.length > 0) {
var replayData = [];
res.data.forEach((item) => {
var arr = [];
arr.push(item.blng);
arr.push(item.blat);
replayData.push(arr);
});
replayList.value = replayData;
initPath(replayData, currentMap);
} else {
replayList.value = [];
Dialog({ message: "暂无轨迹数据" });
}
}
});
};
// 切换速度弹窗
const changeSpeedPickerPopup = () => {
speedId.value = selectSpeedId.value;
showSpeedPicker.value = true;
};
// 切换速度
const changSpeed = (e) => {
speedId.value = e;
};
// 切换速度保存
const saveSpeed = () => {
showSpeedPicker.value = false;
selectSpeedId.value = speedId.value;
if (replay.navg) {
replay.navg.setSpeed(speedEvent(selectSpeedId.value, replay.dis));
if (!isPlay.value) {
play();
}
}
};
// 时间选择保存
const saveTime = () => {
if (!searchTime.value.length) {
return Toast("请选择一个时间段");
}
showTimeDialog.value = false;
let isSelectTime =
timeSelected.value !== null && timeSelected.value.toString();
if (isSelectTime) {
currDateText.value = timeBtnList[timeSelected.value];
getCarReplayData();
} else if (currentDate.value) {
// 不显示picker顶部 确认时调用 自带的confirm 防止选择时间延迟
nextTick(() => {
let confirmBtn = dateTimePicker.value.$el.children[0].children[1];
confirmBtn.click();
});
}
};
// 显示自定义时间
const onShowTimePicker = () => {
showTimePicker.value = true;
timeSelected.value = null;
searchTime.value = [
moment(currentDate.value).format("YYYY-MM-DD") + " 00:00:00",
moment(currentDate.value).format("YYYY-MM-DD") + " 23:59:59",
];
};
// 自定义时间选择
const changeCurrTiem = (e) => {
currentDate.value = e;
timeSelected.value = null;
searchTime.value = [
moment(e).format("YYYY-MM-DD") + " 00:00:00",
moment(e).format("YYYY-MM-DD") + " 23:59:59",
];
currDateText.value = moment(currentDate.value).format("MM-DD");
customTime.value = moment(currentDate.value).format("YYYY-MM-DD");
getCarReplayData();
};
// 查询参数
const getParams = () => {
eParams.value = {
startTime: searchTime.value[0],
endTime: searchTime.value[1],
locationType: 0,
sn: routeQuery.sn,
};
};
// 点击快捷时间选择
const selectTime = (e) => {
timeSelected.value = e;
showTimePicker.value = false;
switch (e) {
case 0:
searchTime.value = [
moment().subtract(1, "day").format("YYYY-MM-DD HH:mm:ss"),
moment().format("YYYY-MM-DD HH:mm:ss"),
];
break;
case 1:
searchTime.value = [
moment().subtract(3, "day").format("YYYY-MM-DD HH:mm:ss"),
moment().format("YYYY-MM-DD HH:mm:ss"),
];
break;
case 2:
searchTime.value = [
moment().subtract(7, "day").format("YYYY-MM-DD HH:mm:ss"),
moment().format("YYYY-MM-DD HH:mm:ss"),
];
break;
}
customTime.value = null;
};
const getSpeed = (idx, dis) => {
// 根据路径距离和回放时间 算出回放的speed
return speedEvent(idx, dis);
};
// 根据距离 实时计算速度
const speedEvent = (idx, dis) => {
let timeArr = [
[40, 30, 16],
[30, 20, 12],
[20, 12, 8],
[5, 3, 2],
]; // 慢 正常 快 很快
let speed;
if (dis >= 100) {
// 100公里以上
speed = Math.floor((dis / timeArr[idx][0]) * 3600);
} else if (dis > 10 && dis < 100) {
// 10-100公里
speed = Math.floor((dis / timeArr[idx][1]) * 3600);
} else {
// 10公里以内
speed = Math.floor((dis / timeArr[idx][2]) * 3600);
}
return speed;
};
const initPath = (data, currentMap) => {
var replayData = [];
data.forEach(function (item) {
let lnglat = mapContentRefs.value.getLngLat(item[0], item[1]);
replayData.push(lnglat);
});
let p1 = replayData[0];
let p2 = replayData[replayData.length - 1];
let line = window.mapapi.invoke("Polyline", replayData, {
strokeColor: "#EC8788",
strokeWeight: 6,
});
// replay.startMarker = mapService.createStartMarker(p1, window.mapapi);
// replay.endMarker = mapService.createEndMarker(p2, window.mapapi);
replay.dis = window.mapapi.invoke("getDistance", p1, p2) / 1000; // 算出轨迹的距离(km)
window.mapapi.invoke("setViewport", replayData);
window.mapapi.invoke("addOverlay", line);
// window.mapapi.invoke("addOverlay", replay.startMarker);
// window.mapapi.invoke("addOverlay", replay.endMarker);
const firstItem = data[0];
replay.navg = window.mapapi.invoke("LuShu", replayData, {
// defaultContent: getNavgInfoContent(firstItem),
autoView: true, // 是否开启自动视野调整,如果开启那么路书在运动过程中会根据视野自动调整
speed: getSpeed(replay.speed, replay.dis),
// speed: 300,
icon: window.mapapi.invoke(
"Icon",
require("@/assets/images/car0.png"),
window.mapapi.invoke("Size", 26, 17),
// { anchor: window.mapapi.invoke("Size", 24, 24) }
),
enableRotation: true, // 是否设置marker随着道路的走向进行旋转
});
// window.mapapi.invoke("lushuStart"); //开始
// AMap.invoke('lushuStop') //停止
// AMap.invoke('lushuPause') //暂停
// AMap.invoke('lushuShowInfoWindow') //打开信息窗口
// EventBus.$emit("uiMap.replay.initDone");
window.mapapi.invoke("lushuAddEventListener", "move", (e) => {
if(e.target.i){
idx.value = e.target.i
}
// let lnglat = replayData[idx.value];
// let item = data[idx.value];
// window.mapapi.invoke("lushuSetContent", replay.getNavgInfoContent(item)); //设置信息窗口文案
// EventBus.$emit("uiMap.replay.currAddress", "");
// console.log(idx.value, data.length - 1);
// if (idx.value >= data.length - 2) {
// EventBus.$emit("uiMap.replay.done");
// }
});
window.mapapi.invoke("lushuAddEventListener", "end", (e) => {
isPlay.value = false;
})
// replay.dis = window.AMap.GeometryUtil.distanceOfLine(replayData) / 1000; // 算出轨迹的距离(km)
// if (replay.PathS) {
// replay.pathSimplifierIns = new replay.PathS({
// zIndex: 100,
// map: currentMap, //所属的地图实例
// getPath: function (pathData) {
// return pathData.path;
// },
// renderOptions: {
// //轨迹线的样式
// pathLineStyle: {
// strokeStyle: "#EC8788",
// lineWidth: 6,
// dirArrowStyle: true,
// },
// pathLineSelectedStyle: {
// strokeStyle: "#EC8788",
// lineWidth: 6,
// },
// },
// });
// replay.pathSimplifierIns.setData([
// {
// path: replayData,
// },
// ]);
// replay.pathSimplifierIns.show();
// replay.speed = speedEvent(speedId.value, replay.dis);
// //创建一个巡航器
// replay.navg = replay.pathSimplifierIns.createPathNavigator(
// 0, //关联第1条轨迹
// {
// loop: false, //循环播放
// speed: speedEvent(speedId.value, replay.dis),
// pathNavigatorStyle: {
// width: 17,
// height: 26,
// //使用图片
// content: replay.PathS.Render.Canvas.getImageContent(carPng),
// initRotateDegree: 180,
// },
// }
// );
// replay.navg.on("pause", function () {
// isPlay.value = false;
// });
// } else {
// window.AMapUI.load(
// ["ui/misc/PathSimplifier"],
// function (PathSimplifier) {
// if (!PathSimplifier.supportCanvas) {
// alert("当前环境不支持 Canvas!");
// return;
// }
// replay.PathS = PathSimplifier;
// initPath(replayData, currentMap);
// }
// );
// }
};
// const getNavgInfoContent = (item) => {
// return `<div class="navg-info-window">
// <div class="tit">${item.dateTime}</div>
// <div class="main">
// <p><span class="lb">经度</span>${item.blng}</p>
// <p><span class="lb">纬度</span>${item.blat}</p>
// <p><span class="lb">速度</span>${item.speed} km/h</p>
// </div>
// </div>`;
// };
// 播放轨迹
const play = () => {
if (!replayList.value.length) {
return Toast("暂无轨迹数据");
}
if (replay.navg) {
if (idx.value === replayList.value.length - 1) {
window.mapapi.invoke("lushuStop");
window.mapapi.invoke("lushuStart"); //开始
} else {
window.mapapi.invoke("lushuStart");
isPlay.value = true;
}
}
};
// 暂停
const pause = () => {
if (replay.navg) {
// replay.navg.pause();
window.mapapi.invoke('lushuPause') //暂停
isPlay.value = false;
}
};
// 重新播放
const replayPath = () => {
if (!replayList.value.length) {
return Toast("暂无轨迹数据");
}
if (replay.navg) {
// replay.navg.start();
window.mapapi.invoke("lushuStart")
isPlay.value = true;
}
};
return {
mapContentRefs,
showSpeedPicker,
speedItemText,
speedId,
refreshCb,
getCarReplayData,
getParams,
timeSelected,
showTimeDialog,
showTimePicker,
currentDate,
timeBtnList,
selectTime,
customTime,
initPath,
speedEvent,
play,
pause,
replayPath,
isPlay,
changSpeed,
saveSpeed,
selectSpeedId,
saveTime,
changeCurrTiem,
currDateText,
onShowTimePicker,
maxDate,
replayList,
dateTimePicker,
changeSpeedPickerPopup,
};
},
});
</script>
<style lang="scss">
.vue-slider-process {
background: var(--base-color);
}
.vue-slider-rail {
background: #e4e7eb;
}
.van-picker__toolbar {
display: none;
}
</style>
<style lang="scss" scoped>
.info-content {
.map-box {
height: calc(100% - 39px) !important;
}
}
.tab {
.tabbar {
font-size: 0px;
display: flex;
position: relative;
font-size: 14px;
color: #111111;
z-index: 1000;
.tabbar-item {
flex: 1;
font-size: 14px;
display: flex;
align-items: center;
justify-content: center;
padding: 10px 0;
.slect-class {
color: var(--base-color);
}
.icon-rotate {
transform: rotate(90deg);
font-size: 12px;
color: #9ca4ad;
margin-left: 5px;
}
}
}
}
.time-content {
background: #fff;
color: #999;
overflow: hidden;
box-sizing: border-box;
width: 100%;
padding: 0 20px;
.time-btn-list {
display: flex;
justify-content: space-between;
text-align: center;
align-items: center;
padding: 20px 0;
font-size: 14px;
border-bottom: 1px solid #f0f2f5;
.label {
width: 60px;
color: #000;
font-weight: 600;
}
.time_btn {
border: 1px solid #dfe2e6;
color: #111;
border-radius: 5px;
width: 80px;
height: 34px;
line-height: 34px;
}
.active {
border: 1px solid var(--base-color);
background: #ebf5ff;
color: var(--base-color);
}
}
.custom-time {
display: flex;
align-items: center;
justify-content: space-between;
justify-items: end;
font-size: 14px;
padding: 20px 0;
.label {
width: 96px;
color: #000;
font-weight: 600;
flex: 1;
}
div {
flex: 1;
display: flex;
justify-content: flex-end;
align-items: center;
}
}
}
.custom-step {
position: absolute;
top: -4px;
height: 6px;
width: 6px;
transform: translate(-50%);
border-radius: 6px;
background-color: #fff;
border: 2px solid #e4e7eb;
}
.custom-step.active {
background-color: #fff;
border: 2px solid var(--base-color);
// box-shadow: 0 0 0 3px var(--base-color);
}
.vue-slider-mark-label {
font-size: 14px;
color: #111;
}
.speedSelect-box {
display: flex;
width: 100%;
align-items: center;
background: #fff;
flex-flow: column;
border-radius: 0 0 16px 16px;
overflow: hidden;
.box {
padding: 20px 40px;
box-sizing: border-box;
width: 100%;
overflow: hidden;
.speed-text {
width: 100%;
position: relative;
font-size: 14px;
.item-text {
position: absolute;
}
.text0 {
left: -3%;
}
.text1 {
left: 27%;
}
.text2 {
left: 64%;
}
.text3 {
right: -5%;
}
}
}
}
.btn-control {
position: fixed;
bottom: 41px;
left: 50%;
margin-left: -72px;
z-index: 101;
a {
border-radius: 10px;
box-shadow: 0px 5px 12px 0px rgba(0, 0, 0, 0.12);
}
.icon-replay-refresh {
margin-left: 12px;
}
}
</style>