【问题标题】:'null is not an object(evaluating'e.region')' react-native-maps. Error is located in the index.android.bundle file'null 不是对象(评估'e.region')' react-native-maps。错误位于 index.android.bundle 文件中
【发布时间】:2017-07-20 13:56:27
【问题描述】:

我正在尝试实现 react-native-maps 并出现此错误。我以前使用过这个组件,没有标题中描述的错误。这都在一个类组件中,然后我将其导出。

componentDidMount() {
    navigator.geolocation.getCurrentPosition(
        ({ coords }) => {
            const { latitude, longitude } = coords

            this.setState({
                position: {
                    latitude,
                    longitude,
                },
                region: {
                    latitude,
                    longitude,
                    latitudeDelta: 0.0922,
                    longitudeDelta: 0.0421,
                },
            })
        },
        (error) => alert(JSON.stringify(error)),
        { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
    )
}

render() {

    const { region, position } = this.state

    return (
        <View>
            <MapView
                style={styles.map}
                region={region}
            >
                {position && (
                    <MapView.Circle
                        center={position}
                        radius={300}
                        strokeColor={'transparent'}
                        fillColor={'rgba(112,185,213,0.30)'}
                    />
                )}
                {position && (
                    <MapView.Circle
                        center={position}
                        radius={100}
                        strokeColor={'transparent'}
                        fillColor={'#3594BC'}
                    />
                )}
            </MapView>
        </View>
    )
}

}

【问题讨论】:

  • 您的默认state 是什么样的?您在componentDidMount 中调用navigator.geolocation.getCurrentPosition,因此根据状态的不同,region 在渲染时可能是null。另请注意,navigator.geolocation.getCurrentPosition 是异步的,因此即使您提前调用它,region 在尝试渲染时仍可能为空。所以你应该默认region 为某个值。
  • 感谢@MichaelCheng,我意识到这是我的状态......只需“state = {}”修复它

标签: android react-native react-native-maps


【解决方案1】:

首先你必须在android中获得位置权限 第二次改变

 { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }

到这里:

{ enableHighAccuracy: false, timeout: 20000, maximumAge: 5000 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    • 2021-08-30
    • 2017-09-19
    相关资源
    最近更新 更多