首页 > 解决方案 > React-Native/Expo Location 状态,如果用户在授予应用权限后不想允许位置

问题描述

在反应原生应用程序中允许访问 GPS 的权限后。如果用户拒绝打开gps。它会显示错误,例如

未处理的承诺拒绝:错误:由于设备设置不满意,位置请求失败。”

如果用户拒绝 Gps 打开选项,我想避免它会返回一些东西。所以我需要该位置的 If 条件,无论它是打开还是关闭。(我正在使用博览会位置)

标签: react-nativegeolocationexpo

解决方案


你看到这个错误是因为 Location.getCurrentPositionAsyncisasync方法并且它返回一个承诺,如果它失败它会抛出一个错误(你看到的错误)。

您可以将代码包装在 atrycatch块中以捕获错误并对其进行处理。例子:

_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();

推荐阅读