-
Notifications
You must be signed in to change notification settings - Fork 104
/
index.js
143 lines (132 loc) · 3.9 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import * as React from "react";
import {
AppRegistry,
Button,
PermissionsAndroid,
Platform,
ScrollView,
StyleSheet,
Text,
View,
} from "react-native";
import {
Geolocation,
init,
setInterval,
setLocatingWithReGeocode,
setNeedAddress,
} from "react-native-amap-geolocation";
const style = StyleSheet.create({
body: {
padding: 16,
paddingTop: Platform.OS === "ios" ? 48 : 16,
},
controls: {
flexWrap: "wrap",
alignItems: "flex-start",
flexDirection: "row",
marginBottom: 16,
},
button: {
flexDirection: "column",
marginRight: 8,
marginBottom: 8,
},
result: {
fontFamily: Platform.OS === "ios" ? "menlo" : "monospace",
},
});
class App extends React.Component {
state = { location: null };
async componentDidMount() {
if (Platform.OS === "android") {
const result = await PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION,
]);
console.log(result);
}
await init({
ios: "d258237d14f75e2e1bbb1654a22060cf",
android: "c52c7169e6df23490e3114330098aaac",
});
}
updateLocationState(location) {
if (location) {
this.setState({ location });
console.log(location);
}
}
getCurrentPosition = () => {
Geolocation.getCurrentPosition(
(position) => this.updateLocationState(position),
(error) => this.updateLocationState(error)
);
};
watchPosition = () => {
if (!this.watchId) {
this.watchId = Geolocation.watchPosition(
(position) => this.updateLocationState(position),
(error) => this.updateLocationState(error)
);
}
};
clearWatch = () => {
if (this.watchId) {
Geolocation.clearWatch(this.watchId);
this.watchId = null;
}
this.setState({ location: null });
};
setInterval2000 = () => setInterval(2000);
setInterval10000 = () => setInterval(10000);
setNeedAddressTrue = () => setNeedAddress(true);
setNeedAddressFalse = () => setNeedAddress(false);
setLocatingWithReGeocodeTrue = () => setLocatingWithReGeocode(true);
setLocatingWithReGeocodeFalse = () => setLocatingWithReGeocode(false);
render() {
const { location } = this.state;
return (
<ScrollView contentContainerStyle={style.body}>
<View style={style.controls}>
<View style={style.button}>
<Button onPress={this.getCurrentPosition} title="Geolocation.getCurrentPosition" />
</View>
<View style={style.button}>
<Button onPress={this.watchPosition} title="Geolocation.watchPosition" />
</View>
<View style={style.button}>
<Button onPress={this.clearWatch} title="Geolocation.clearWatch" />
</View>
<View style={style.button}>
<Button onPress={this.setInterval2000} title="setInterval(2000)" />
</View>
<View style={style.button}>
<Button onPress={this.setInterval10000} title="setInterval(10000)" />
</View>
<View style={style.button}>
<Button onPress={this.setNeedAddressTrue} title="setNeedAddress(true)" />
</View>
<View style={style.button}>
<Button onPress={this.setNeedAddressFalse} title="setNeedAddress(false)" />
</View>
<View style={style.button}>
<Button
onPress={this.setLocatingWithReGeocodeTrue}
title="setLocatingWithReGeocode(true)"
/>
</View>
<View style={style.button}>
<Button
onPress={this.setLocatingWithReGeocodeFalse}
title="setLocatingWithReGeocode(false)"
/>
</View>
</View>
<Text style={style.result}>{`${JSON.stringify(location, null, 2)}
`}</Text>
</ScrollView>
);
}
}
AppRegistry.registerComponent("example", () => App);