-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
127 lines (108 loc) · 3.57 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import React, { Component } from 'react';
import { Button, Platform, ToastAndroid, StyleSheet, Text, View, PermissionsAndroid} from 'react-native';
import Geolocation from 'react-native-geolocation-service';
export default class App extends Component {
watchId = null;
state = {
loading: false,
updatesEnabled: false,
location: {}
};
hasLocationPermission = async () => {
if (Platform.OS === 'ios' ||
(Platform.OS === 'android' && Platform.Version < 23)) {
return true;
}
const hasPermission = await PermissionsAndroid.check(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
);
if (hasPermission) return true;
const status = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
);
if (status === PermissionsAndroid.RESULTS.GRANTED) return true;
if (status === PermissionsAndroid.RESULTS.DENIED) {
ToastAndroid.show('Location permission denied by user.', ToastAndroid.LONG);
} else if (status === PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN) {
ToastAndroid.show('Location permission revoked by user.', ToastAndroid.LONG);
}
return false;
}
getLocation = async () => {
const hasLocationPermission = await this.hasLocationPermission();
if (!hasLocationPermission) return;
this.setState({ loading: true }, () => {
Geolocation.getCurrentPosition(
(position) => {
this.setState({ location: position, loading: false });
console.log(position);
},
(error) => {
this.setState({ location: error, loading: false });
console.log(error);
},
{ enableHighAccuracy: true, timeout: 15000, maximumAge: 1000, distanceFilter: 5 }
);
});
}
getLocationUpdates = async () => {
const hasLocationPermission = await this.hasLocationPermission();
if (!hasLocationPermission) return;
this.setState({ updatesEnabled: true }, () => {
this.watchId = Geolocation.watchPosition(
(position) => {
this.setState({ location: position });
console.log(position);
},
(error) => {
this.setState({ location: error });
console.log(error);
},
{ enableHighAccuracy: true, distanceFilter: 0, interval: 1000, fastestInterval: 1000 }
);
});
}
removeLocationUpdates = () => {
if (this.watchId !== null) {
Geolocation.clearWatch(this.watchId);
this.setState({ updatesEnabled: false })
}
}
render() {
const { loading, location, updatesEnabled } = this.state;
return (
<View style={styles.container}>
<Button title='Get Location' onPress={this.getLocation} disabled={loading || updatesEnabled} />
<View style={styles.buttons}>
<Button title='Start Observing' onPress={this.getLocationUpdates} disabled={updatesEnabled} />
<Button title='Stop Observing' onPress={this.removeLocationUpdates} disabled={!updatesEnabled} />
</View>
<View style={styles.result}>
<Text>{JSON.stringify(location, null, 4)}</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
paddingHorizontal: 12
},
result: {
borderWidth: 1,
borderColor: '#666',
width: '100%',
paddingHorizontal: 16
},
buttons: {
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'center',
marginVertical: 12,
width: '100%'
}
});