forked from qiuxiang/react-native-amap-geolocation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
148 lines (136 loc) · 3.98 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
144
145
146
147
148
import React from "react";
import {
AppRegistry,
Button,
StyleSheet,
Text,
View,
ScrollView,
PermissionsAndroid,
Platform,
} from "react-native";
import {
init,
Geolocation,
setInterval,
setNeedAddress,
setLocatingWithReGeocode,
} from "./src";
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: "9bd6c82e77583020a73ef1af59d0c759",
android: "043b24fe18785f33c491705ffe5b6935",
});
}
componentWillUnmount() {
stop();
}
updateLocationState(location) {
if (location) {
location.updateTime = new Date().toLocaleString();
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("RNAMapGeolocation", () => App);