`import React from "react";
import { AppRegistry, Button, StyleSheet, Text, View } 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() {
await Geolocation.init({
ios: "9bd6c82e77583020a73ef1af59d0c759",
android: "043b24fe18785f33c491705ffe5b6935"
});
Geolocation.setOptions({
interval: 10000,
distanceFilter: 10,
background: true,
reGeocode: true
});
Geolocation.addLocationListener(location =>
this.updateLocationState(location)
);
}
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 (
{Object.keys(location).map(key => (
{key}
{location[key]}
))}
);
}
}
AppRegistry.registerComponent("RNAMapGeolocation", () => App);
`
手动调用getLastLocation()方法,无法获取返回信息