-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
103 lines (93 loc) · 3.05 KB
/
App.js
File metadata and controls
103 lines (93 loc) · 3.05 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
// @flow
import React, {Component} from 'react';
import { StyleSheet, Text, View, PermissionsAndroid, TouchableOpacity } from 'react-native';
import LocationService, { PriorityModeEnum } from 'react-native-android-background-geolocation';
export default class App extends Component<{}> {
async componentDidMount() {
// 1. Request permissions
await this._requestLocationPermission();
// 2. Register a event listener by OnError and onLocationChanged
LocationService.onError(({code, message}) => {
console.log('error code', code);
console.log('error message', message);
});
LocationService.onLocationChanged(({coords}) => console.log('coords', coords));
}
componentWillUnmount() {
// 3. Remove All listeners
LocationService.removeAllListeners();
}
async _requestLocationPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
'title': 'App Location Permission',
'message': 'Just for test.'
}
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('Location permission granted.');
} else {
console.log('Location permission denied.');
}
} catch (error) {
console.warn(error);
}
}
_startGeolocationService() {
LocationService.start({
priority: PriorityModeEnum.PRIORITY_HIGH_ACCURACY,
stopOnTerminate: false,
interval: 20 * 1000,
fastestInterval: 15 * 1000,
distanceFilter: 100,
});
}
_stopGeolocationService() {
LocationService.stop();
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>Welcome to React Native!</Text>
<TouchableOpacity
style={styles.button}
onPress={() => this._startGeolocationService()}
>
<Text style={styles.buttonText} >Start Location Service</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => this._stopGeolocationService()}
>
<Text style={styles.buttonText} >Stop Location Service</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
button: {
textAlign: 'center',
backgroundColor: '#4289f4',
margin: 10,
paddingVertical: 10,
paddingHorizontal: 20,
},
buttonText: {
color: '#ffffff',
fontSize: 18,
}
});