我有一個MapView,我試圖專注於用戶的當前位置。現在我正在記錄位置,並記錄currentRegion狀態。我的問題是組件顯示日誌中的空數組,並且在它通過渲染循環之後,它將狀態設置爲位置座標,以便MapView顯示我在海洋中間的某處。爲什麼initialRegion沒有抓住變化的狀態和渲染?React本地問題與狀態和MapView組件中的initialRegion
const screen = Dimensions.get('window');
const ASPECT_RATIO = screen.width/screen.height;
var LATITUDE_DELTA = 0.3022;
var LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO;
class MapScreen extends Component {
constructor(props) {
super(props)
this.state = {
currentRegion: []
}
}
componentDidMount() {
navigator.geolocation.getCurrentPosition(
(position) => {
console.log(position)
this.setState({ currentRegion: {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
}})
},
(error) => alert(error.message),
{timeout: 20000, maximumAge: 1000}
)
}
render() {
console.log(this.state.currentRegion)
return(
<View style={styles.container}>
<MapView
showsUserLocation = {true}
style={styles.map}
initialRegion={this.state.currentRegion}
/>
</View>
)
}
}