菜鸟问题,但找不到好的答案, 我试图只在不同的字符串中获取经度和纬度。目前它有效但它记录了这个:
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;
`;
我要导出经度和纬度。我知道代码有点乱。非常感谢任何建议。
最佳答案
不需要解析JSON,已经是对象了:
const location = await Location.getCurrentPositionAsync();
this.setState({
location : {
lat: location.coords.latitude,
lon: location.coords.longitude
}
});
渲染:
<View>
<Text>{this.state.location.lat} {this.state.location.lon} </Text>
</View>
关于javascript - 如何从数组中仅获取经度和纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59453303/