-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathDriverMarker.tsx
More file actions
77 lines (64 loc) · 2.5 KB
/
DriverMarker.tsx
File metadata and controls
77 lines (64 loc) · 2.5 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
import React, { useRef, useEffect, useCallback } from 'react';
import { useFocusEffect } from '@react-navigation/native';
import TrackingMarker from './TrackingMarker';
import useSocketClusterClient from '../hooks/use-socket-cluster-client';
import useEventBuffer from '../hooks/use-event-buffer';
const DriverMarker = ({ driver, onPositionChange, onHeadingChange, onMovement, ...props }) => {
const markerRef = useRef();
const listenerRef = useRef();
const handleEvent = useCallback((data) => {
console.log('Incoming data:', data);
const movementData = { data };
if (data.location && data.location.coordinates) {
const [latitude, longitude] = data.location.coordinates;
if (markerRef.current) {
markerRef.current.move(latitude, longitude);
}
if (typeof onPositionChange === 'function') {
onPositionChange({ latitude, longitude });
}
movementData = { ...movementData, coordinates: { latitude, longitude } };
}
if (typeof data.heading === 'number') {
if (markerRef.current) {
markerRef.current.rotate(data.heading);
}
if (typeof onPositionChange === 'function') {
onHeadingChange(data.heading);
}
movementData = { ...movementData, heading: data.heading };
}
if (typeof onMovement === 'function') {
onMovement(movementData);
}
}, []);
const { listen } = useSocketClusterClient();
const { addEvent, clearEvents } = useEventBuffer(handleEvent);
useFocusEffect(
useCallback(() => {
const trackDriverMovement = async () => {
const listener = await listen(`driver.${driver.id}`, (event) => addEvent(event));
if (listener) {
listenerRef.current = listener;
}
};
trackDriverMovement();
return () => {
if (listenerRef.current) {
listenerRef.current.stop();
}
clearEvents();
};
}, [listen, driver.id])
);
return (
<TrackingMarker
ref={markerRef}
coordinate={{ latitude: driver.latitude, longitude: driver.longitude }}
imageSource={{ uri: driver.getAttribute('vehicle_avatar') }}
size={{ width: 50, height: 50 }}
{...props}
/>
);
};
export default DriverMarker;