Android设置状态数组不起作用。 React-Native MapView componentWillMount更新状态设置的地图位置不起作用。请帮助我。
状态:
constructor(props) {
super(props);
this.state = {
loading: '',
mapRegion: {
latitude: 37.78825,
longitude: -122.4324,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
},
location: {
coords: {
latitude: 37.78825,
longitude: -122.4324,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}
}
};
}
组件将安装
async componentWillMount() {
this.setState({
mapRegion: {
latitude: responseJson.latitude,
longitude: responseJson.longitude,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
},
location: {
coords: {
latitude: responseJson.latitude,
longitude: responseJson.longitude,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}
},
});
}
返回视图不会改变位置
<MapView style={{ width: '100%', height: '100%' }}
region={{
latitude: this.state.location.coords.latitude,
longitude: this.state.location.coords.longitude,
latitudeDelta: 3,
longitudeDelta: 3
}}
zoomEnabled = {true}>
<MapView.Marker coordinate={this.state.location.coords} title={strings('Location.Buradasiniz')} />
<MapView.Circle center={this.state.location.coords} radius={this.state.radius} strokeColor='red' fillColor="rgba(255, 0, 0, 0.20)"/></MapView>
答案 0 :(得分:0)
尝试一下:
async componentWillMount() {
await this.setState({
mapRegion: {
latitude: responseJson.latitude,
longitude: responseJson.longitude,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
},
location: {
coords: {
latitude: responseJson.latitude,
longitude: responseJson.longitude,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}
},
});
}
此后使用async
时,您必须在某个地方写await
,这是文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
或者尝试不使用async await
,例如:
componentWillMount() {
this.setState({
mapRegion: {
latitude: responseJson.latitude,
longitude: responseJson.longitude,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
},
location: {
coords: {
latitude: responseJson.latitude,
longitude: responseJson.longitude,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}
},
});
}
祝你好运))