Location.requestForegroundPermissionsAsync 不是函数

Location.requestForegroundPermissionsAsync is not a function

我刚刚尝试使用 expo-location,但我发现了那个错误 Location.requestForegroundPermissionsAsync 不是函数

这是我的代码

import * as Location from 'expo-location';

const setCurrentLocation = async() => {
        let { status } = await Location.requestForegroundPermissionsAsync();
        if (status !== 'granted') {
            setErrorMsg('Permission to access location was denied');
            return;
          }
          let location = await Location.getCurrentPositionAsync({});
          setLocation(location);
            if (errorMsg) {
                setCheckText(errorMsg)
            } else if (location) {
                setCheckText(JSON.stringify(location))
            }  
    }

显示坐标-

像这样初始化你的state

  const [location, setLocation] = React.useState(null);

让你的 setCurrentLocation 函数像这样

  const setCurrentLocation = async () => {
    let { status } = await Location.requestForegroundPermissionsAsync();
    if (status !== 'granted') {
      setErrorMsg('Permission to access location was denied');
      return;
    }
    let location = await Location.getCurrentPositionAsync({});
    setLocation(location);
  }; 

那么您的 return 部分将如下所示

return (
    <View style={styles.container}>
      {location ? <Text>latitude = {location.coords.latitude}</Text> : null}
      {location ? <Text>longitude = {location.coords.longitude}</Text> : null}
    </View>
  );

Working Example