问题描述
我只是尝试使用 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>
);