-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
60 lines (47 loc) · 1.55 KB
/
App.js
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
import React, { useEffect, useState } from 'react';
import Map from './components/Map/Map';
import * as Location from 'expo-location';
export default function App() {
const [userLocation, setUserLocation] = useState({
coords: {
latitude: 55.9533,
longitude: -3.1883,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421
}
})
useEffect(() => {
const startLocationUpdates = async () => {
try {
let { status } = await Location.requestForegroundPermissionsAsync()
if (status === 'granted') {
console.log('Permission granted!')
const initialLocation = await Location.getCurrentPositionAsync()
setUserLocation(initialLocation)
// start location updates
Location.startLocationUpdatesAsync('locationUpdates', {
accuracy: Location.Accuracy.Highest,
distanceInterval: 10, // Updates location every 10 meters
timeInterval: 1000,
})
// receive location updates
Location.EventEmitter.addListener('Expo.locationChanged', (event) => {
setUserLocation(event)
})
} else {
console.log('Permission was denied')
}
} catch (error) {
console.error('Error getting location permissions:', error)
}
}
startLocationUpdates()
// stop location updates when the component unmounts
return () => {
Location.stopLocationUpdatesAsync('locationUpdates')
}
}, [])
return (
<Map userLocation={userLocation} />
)
}