无法获得适当的纬度和长时间响应本机android expo-location

问题描述

我是初学者,正在尝试在React-Native android应用中显示当前位置,但是我无法顺着经度和纬度进行操作,但会出现错误:typeError null不是一个对象(正在评估location.coords)。我的代码是:

import React,{useState,useEffect} from 'react';
import {StyleSheet} from 'react-native';
import MapView from 'react-native-maps';
import * as Location from 'expo-location';


function HomeScreen({navigation}) {
    const [location,setLocation] = useState(null);
    const [errorMsg,setErrorMsg] = useState(null);

    useEffect(() => {
        (async () => {
            let {status} = await Location.requestPermissionsAsync();
            if (status !== 'granted') {
                setErrorMsg('Permission to access location was denied');
            }

            let location = await Location.getCurrentPositionAsync({});
            setLocation(location);

        })();
    });





    return (
        <MapView
            style={styles.map}
            initialRegion={{
                latitude:parseFloat(location.coords.latitude),longitude: parseFloat(location.coords.longitude),latitudeDelta: 0.0922,longitudeDelta: 0.0421,}}>
        </MapView>
    );
}

const styles = StyleSheet.create({
    map: {
        ...StyleSheet.absoluteFillObject
    }
})

export default HomeScreen

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)