Location.requestForegroundPermissionsAsync 不是一个函数

问题描述

我只是尝试使用 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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...