[Noob问题,但找不到一个好的答案,我试图只在不同的字符串中仅获取经度和纬度。目前它可以正常工作,但它记录以下内容:
Object {
"coords": Object {
"accuracy": 5,
"altitude": 0,
"altitudeAccuracy": -1,
"heading": -1,
"latitude": 37.785834,
"longitude": -122.406417,
"speed": -1,
},
"timestamp": 1577094980806.544,
这是代码:
import styled from "styled-components";
import * as Permissions from "expo-permissions";
import * as Location from "expo-location";
export default class GetLocation extends React.Component {
state = {
location: {},
errorMessage: ""
};
constructor(props) {
super(props);
this.state = {
longitude: [],
latitude: []
};
}
componentDidMount() {
this._getLocation();
}
_getLocation = async () => {
const { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== "granted") {
console.log("PERMISSION NOT GRANTED!");
this.setState({
errorMessage: "PERMISSION NOT GRANTED"
});
}
const location = await Location.getCurrentPositionAsync();
this.setState({
location
});
return location.JSON;
};
render() {
console.log(this.state.location);
return (
<View>
<Text>{JSON.stringify(this.state.location)}</Text>
</View>
);
}
}
const View = styled.View``;
const Text = styled.Text`
font-size: 12px;
color: black;
`;
我要导出经度和纬度。我知道代码有点混乱。任何建议都将受到高度赞赏。
您是否尝试过这种方式:
console.log(this.state.location.coords.latitude);