我使用此代码从用户获取当前位置,但现在我在使用setState来保存纬度和经度状态时会有通知。
ReferenceError:未定义setState
async componentDidMount(){
const { status } = await Expo.Permissions.askAsync(Expo.Permissions.LOCATION);
if (status === 'granted') {
const location = await Expo.Location.getCurrentPositionAsync({
enableHighAccuracy: true,
});
setState({
lat:JSON.stringify(location.coords.latitude),
long:JSON.stringify(location.coords.longitude),
});
}
}
如何将位置保存到州?
你可以用this
setState。
在你的constructor
添加状态变量,
constructor(props) {
super(props);
this.state = {
...
lat: 0.000,
long: 0.000,
...
};
}
然后将this
添加到方法中。
async componentDidMount(){
const { status } = await Expo.Permissions.askAsync(Expo.Permissions.LOCATION);
if (status === 'granted') {
const location = await Expo.Location.getCurrentPositionAsync({
enableHighAccuracy: true,
});
this.setState({
lat:JSON.stringify(location.coords.latitude),
long:JSON.stringify(location.coords.longitude),
});
}
}