-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapScreen.tsx
More file actions
62 lines (55 loc) · 1.5 KB
/
MapScreen.tsx
File metadata and controls
62 lines (55 loc) · 1.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
import {
Camera,
type CameraRef,
LocationManager,
Map as MapLibreMap,
UserLocation,
useCurrentPosition,
} from '@maplibre/maplibre-react-native';
import {useEffect, useRef, useState} from 'react';
import {StyleSheet, View} from 'react-native';
const MAP_STYLE = 'https://tiles.openfreemap.org/styles/liberty';
const USER_ZOOM = 14;
export function MapScreen() {
const cameraRef = useRef<CameraRef>(null);
const hasCenteredRef = useRef(false);
const [permissionGranted, setPermissionGranted] = useState(false);
useEffect(() => {
let cancelled = false;
(async () => {
const granted = await LocationManager.requestPermissions();
if (!cancelled && granted) {
setPermissionGranted(true);
}
})();
return () => {
cancelled = true;
};
}, []);
const position = useCurrentPosition({enabled: permissionGranted});
useEffect(() => {
if (hasCenteredRef.current || !position) return;
hasCenteredRef.current = true;
cameraRef.current?.flyTo({
center: [position.coords.longitude, position.coords.latitude],
zoom: USER_ZOOM,
duration: 1500,
});
}, [position]);
return (
<View style={styles.container}>
<MapLibreMap mapStyle={MAP_STYLE} style={styles.map}>
<Camera ref={cameraRef} initialViewState={{center: [0, 20], zoom: 1}} />
<UserLocation animated accuracy />
</MapLibreMap>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
map: {
flex: 1,
},
});