google-maps - Location.requestForegroundPermissionsAsync 不是函数
问题描述
我只是尝试使用 expo-location,发现错误 Location.requestForegroundPermissionsAsync is not a function
这是我的代码
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>
);
推荐阅读
- activemq-artemis - 大消息因转移而丢失
- java - 如何在外部 jar 导入中修复 Autowired
- mysql - IF 附近无法识别的语句 - 使用 IF 与 Exists
- batch-file - 在多个随机文件夹中搜索最新文件并将其复制到网络位置
- react-native - 状态更改时 React-Native-Component 不呈现
- java - 使用 Runtime.getRuntime().exec 执行 cmd 时如何隐藏 cmd 窗口
- php - 在 CENTOS 下运行 CRON JOBS
- angular - Angular - 多个订阅序列不起作用
- flutter - Flutter Navigation Drawer ExpansionTile 在下一个选项被选中时不会折叠以前打开的选项
- .net - 使用 C# 提取 7z SFX 文件