react-native - React-Native/Expo Location 状态,如果用户在授予应用权限后不想允许位置
问题描述
在反应原生应用程序中允许访问 GPS 的权限后。如果用户拒绝打开gps。它会显示错误,例如
未处理的承诺拒绝:错误:由于设备设置不满意,位置请求失败。”
如果用户拒绝 Gps 打开选项,我想避免它会返回一些东西。所以我需要该位置的 If 条件,无论它是打开还是关闭。(我正在使用博览会位置)
解决方案
你看到这个错误是因为 Location.getCurrentPositionAsync
isasync
方法并且它返回一个承诺,如果它失败它会抛出一个错误(你看到的错误)。
您可以将代码包装在 atry
和catch
块中以捕获错误并对其进行处理。例子:
_getLocationAsync = async () => {
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== 'granted') {
alert('The request was denied');
}else{
try{
let location = await Location.getCurrentPositionAsync({});
// do something with location
}catch(e){
alert('We could not find your position. Please make sure your location service provider is on');
console.log('Error while trying to get location: ', e);
}
}
}
// call this._getLocationAsync();
推荐阅读
- php - “img”标签如何显示正在运行的视频(很想知道)
- c++ - 如何利用 fstream 从 .txt 文件 C++ 中取出 int 值
- math - 如何解决两个圆相交点垂直的问题?
- c++ - 如何返回使用参数包定义的成员函数的 std::function 对象?
- django - Django CMS 如何写博客
- android - 如何在 Unity 中使用 Google 帐户对用户进行身份验证?
- xaml - TabView 的子项中触发了哪些事件?
- angular - 您如何为 Angular 8 中的 HTML5 画布动态设置 clientHeight 和 clientWidth?
- modelica - 搜索 Modelica.Userguide
- php - 如何将 laravel 项目从本地部署到服务器