gpt4 book ai didi

javascript - React-native, map , Prop 'region.latitude'在 'MapView'中被标记为必需,但它的值为空

转载 作者:行者123 更新时间:2023-12-03 09:01:14 24 4
gpt4 key购买 nike

我在尝试获取世博会中的当前位置时收到此错误。我在 console.log 中得到了正确的值,所以我不知道问题是什么。在 Expo 中,它说“执行 UI block 时抛出异常,NSNull 双值:无法识别的选择器发送到实例”。这是我的代码:

this.state = { text: 'Destination' ,
latitude: null,
longitude: null,
error: null,

componentDidMount() {
navigator.geolocation.getCurrentPosition(
(position) => {
this.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
error: null,
});
},
(error) => this.setState({ error: error.message }),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 },
);

{.....}
render()
<MapView style={styles.map}
region ={{
latitude:this.state.latitude,
longitude:this.state.longitude,
latitudeDelta: 0.1,
longitudeDelta: 0.1,

最佳答案

getCurrentPosition() 是异步的,因此您的初始 latitude 状态为 null。

要么为其提供默认的非空值,要么避免渲染MapView,直到它不再为空:

render() {
return this.state.latitude !== null && <MapView ... />;
}

关于javascript - React-native, map , Prop 'region.latitude'在 'MapView'中被标记为必需,但它的值为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49976234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com