useMapTrackPlayback.ts
2.88 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
import { ShallowRef, computed, ref, shallowRef, unref } from 'vue';
import { TrackAnimationStatus } from './type';
import { useTimeoutFn } from '@vueuse/core';
import { useMessage } from '/@/hooks/web/useMessage';
export const useMapTrackPlayback = (mapInstance: ShallowRef<Nullable<Recordable>>) => {
const { createMessage } = useMessage();
const trackAni = ref<Nullable<Recordable>>(null);
const playFn = shallowRef<Nullable<Fn>>(() => {
trackAni.value?.start?.();
});
const pauseFn = shallowRef<Nullable<Fn>>(() => {
trackAni.value?.pause?.();
});
const continueFn = shallowRef<Nullable<Fn>>(() => {
trackAni.value?.continue?.();
});
const playStatus = computed<TrackAnimationStatus>(() => {
return unref(trackAni)?._status;
});
const RANDOM_PATH = [
{
lng: 116.297611,
lat: 40.047363,
},
{
lng: 116.302839,
lat: 40.048219,
},
{
lng: 116.308301,
lat: 40.050566,
},
{
lng: 116.305732,
lat: 40.054957,
},
{
lng: 116.304754,
lat: 40.057953,
},
{
lng: 116.306487,
lat: 40.058312,
},
{
lng: 116.307223,
lat: 40.056379,
},
];
const getAnimationDurationTime = (length: number) => {
const itemDurationTime = 1000;
const minDurationTime = 5000;
const durationTime = length * itemDurationTime;
return Math.max(durationTime, minDurationTime);
};
/**
* @demo https://lbsyun.baidu.com/jsdemo.htm#fAnimationPause
* @param path
* @param clearOverlays
*/
const genTrackPlaybackAnimation = (
path?: Record<'lng' | 'lat', number>[],
clearOverlays = true
) => {
path = path || RANDOM_PATH;
if (!path.length) {
createMessage.warning('无可用数据~');
return;
}
const point: any[] = [];
const BMapGL = (window as any).BMapGL;
clearOverlays && unref(mapInstance)?.clearOverlays();
for (const { lng, lat } of path) {
point.push(new BMapGL.Point(lng, lat));
}
const pl = new BMapGL.Polyline(point);
const BMapGLLib = (window as any).BMapGLLib;
for (const item of path) {
marketPoint(item);
}
const duration = getAnimationDurationTime(path?.length || 0);
const delay = 300;
trackAni.value = new BMapGLLib.TrackAnimation(unref(mapInstance), pl, {
overallView: true,
tilt: 30,
duration,
delay,
});
useTimeoutFn(() => {
unref(playFn)?.();
}, 500);
};
function marketPoint(params: Partial<Record<'lng' | 'lat', number>>) {
const { lng, lat } = params;
if (![lng, lat].every(Boolean)) return;
const BMap = (window as any).BMapGL;
const marker = new BMap.Marker(new BMap.Point(lng, lat));
unref(mapInstance)?.centerAndZoom(new BMap.Point(lng, lat));
unref(mapInstance)?.addOverlay(marker);
}
return { genTrackPlaybackAnimation, playFn, pauseFn, continueFn, playStatus };
};