forked from qiuxiang/react-native-amap-geolocation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (78 loc) · 2.28 KB
/
index.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
import React from "react";
import { AppRegistry, Button, StyleSheet, Text, View, PermissionsAndroid } from "react-native";
import { Geolocation } from "react-native-amap-geolocation";
const style = StyleSheet.create({
body: {
padding: 16
},
controls: {
flexDirection: "row",
justifyContent: "space-around",
marginTop: 12,
marginBottom: 24
},
item: {
flexDirection: "row",
marginBottom: 4
},
label: {
color: "#f5533d",
width: 120,
paddingRight: 10,
textAlign: "right"
}
});
class App extends React.Component {
state = { location: {} };
async componentDidMount() {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
await Geolocation.init({
ios: "9bd6c82e77583020a73ef1af59d0c759",
android: "043b24fe18785f33c491705ffe5b6935"
});
Geolocation.setOptions({
interval: 10000,
distanceFilter: 10,
background: true,
reGeocode: true
});
Geolocation.addLocationListener(location => this.updateLocationState(location));
} else {
console.error("Location permission denied");
}
}
componentWillUnmount() {
Geolocation.stop();
}
updateLocationState(location) {
if (location) {
location.timestamp = new Date(location.timestamp).toLocaleString();
this.setState({ location });
console.log(location);
}
}
startLocation = () => Geolocation.start();
stopLocation = () => Geolocation.stop();
getLastLocation = async () => this.updateLocationState(await Geolocation.getLastLocation());
render() {
const { location } = this.state;
return (
<View style={style.body}>
<View style={style.controls}>
<Button style={style.button} onPress={this.startLocation} title="开始定位" />
<Button style={style.button} onPress={this.stopLocation} title="停止定位" />
</View>
{Object.keys(location).map(key => (
<View style={style.item} key={key}>
<Text style={style.label}>{key}</Text>
<Text>{location[key]}</Text>
</View>
))}
</View>
);
}
}
AppRegistry.registerComponent("RNAMapGeolocation", () => App);