Массив состояния Android Set не работает. 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>
Попробуй это:
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,
}
},
});
}
Удачи))