拒绝启用位置弹出窗口后做出本机错误

时间:2020-03-15 12:07:37

标签: javascript android react-native mobile weather

因此,我正在尝试制作一个天气应用,当我在启用位置的弹出窗口中拒绝(按NO)时,出现以下错误:[Unhandled promise rejection: Error: Location request failed due to unsatisfied device settings.]

代码的重要部分如下所示。

    let { status } = await Permissions.askAsync(Permissions.LOCATION);
    if (status !== 'granted') {
      this.setState({
        errorMessage: 'Permission to access location was denied, please activate location and reopen the app.',
      });

    }

    let location = await Location.getCurrentPositionAsync({enableHighAccuracy:true});
    let lat = location.coords.latitude;
    let lon = location.coords.longitude;

注意:我使用的是React Native的最新版本,我在android上,为此使用了expo-location,当我启用(按YES)位置时,它可以完美工作。

1 个答案:

答案 0 :(得分:0)

您应该使用try catch块包装您的await呼叫以捕获错误。

try {
  let { status } = await Permissions.askAsync(Permissions.LOCATION);
} catch(err) {
  //handle err
}