-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBackgroundTask.js
49 lines (41 loc) · 1.25 KB
/
BackgroundTask.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
import AsyncStorage from '@react-native-async-storage/async-storage';
import Geolocation from 'react-native-geolocation-service';
const registerLocationUpdates = () => {
return new Promise(async (resolve, reject) => {
try {
const geolocationOptions = {
enableHighAccuracy: true,
distanceFilter: 10,
interval: 1000,
fastestInterval: 10000,
showLocationDialog: true,
};
const watchId = Geolocation.watchPosition(
async position => {
const {latitude, longitude} = position.coords;
try {
const locationData = JSON.stringify({latitude, longitude});
await AsyncStorage.setItem('storedLocation', locationData);
} catch (error) {
console.error('Error storing location data:', error);
}
},
error => {
console.error('Location Error:', error);
},
geolocationOptions,
);
resolve(watchId);
} catch (error) {
reject(error);
}
});
};
const registerBackgroundService = async () => {
try {
const watchId = await registerLocationUpdates();
} catch (error) {
console.error('Background Service Error:', error);
}
};
export default registerBackgroundService;