如何修复[未处理的承诺拒绝:错误:位置提供程序不可用。确保定位服务已启用。]

Suj*_*jay 7 android react-native react-native-maps expo

我刚刚将我的应用程序从 Expo SDK 37.0.0 升级到 38.0.0。该应用程序在 iOS 上运行良好,但在 Android 上我收到以下警告,并且该应用程序无法在地图上对我进行地理定位。

开发环境:

  • Expo SDK 38.0.0(托管工作流程)
  • 反应本机 0.62
  • 地图提供商:Android 版 Google 地图
  • 反应本机地图:0.27.1
  • 反应:16.11.0
  • 设备:荣耀8X/Android 10

预期结果:应用程序应根据我的位置对我(用户)进行地理定位。实际结果:应用程序不会根据我的位置对我(用户)进行地理定位。警告 :

[未处理的承诺拒绝:错误:位置提供商不可用。确保定位服务已启用。]

到目前为止我尝试过什么?

  1. 再次运行 expo 升级以确保所有软件包都设置为正确的版本。

  2. 删除 package-lock.json 和 node_modules 并运行 npm install。

  3. 设置 Location.getCurrentPositionAsync({accuracy:Location.Accuracy.High})

  4. 设置 Location.getCurrentPositionAsync({ enableHighAccuracy: true })

    import * as Location from 'expo-location';
    import * as Permissions from 'expo-permissions';
    
    
    async _getLocationAsync() {
      let { status } = await Permissions.askAsync(Permissions.LOCATION);
      if (status !== 'granted') {
        /* If user hasn't granted permission to geolocate himself herself */
        Alert.alert(
          "User location not detected",
          "You haven't granted permission to detect your location.",
          [{ text: 'OK', onPress: () => console.log('OK Pressed') }]
        );
      }
    
      let location = await Location.getCurrentPositionAsync({ accuracy: Location.Accuracy.High });
      this.setState({
        userLat: location.coords.latitude,
        userLng: location.coords.longitude,
        navGeoStatus: true,
        lat: location.coords.latitude,
        lng: location.coords.longitude
      });
    }```
    
    Run Code Online (Sandbox Code Playgroud)

小智 6

如果有人仍在解决这个问题 - 请检查您是否添加了:

 "android": {
  "permissions": ["ACCESS_BACKGROUND_LOCATION"]
},
Run Code Online (Sandbox Code Playgroud)

在你的app.json文件中


小智 5

改变

let location = await Location.getCurrentPositionAsync({});
Run Code Online (Sandbox Code Playgroud)

为了:

let location = await Location.getLastKnownPositionAsync({
          accuracy: 6,
        });
Run Code Online (Sandbox Code Playgroud)


Kha*_*din -1

用户似乎已授予应用程序使用位置的权限。但当手机本身的定位服务尚未启用时,就会出现此警告。在设置中搜索并启用它

编辑后的答案(添加代码来处理警告):

let gpsServiceStatus = await Location.hasServicesEnabledAsync();
      if (gpsServiceStatus) {
let location = await Location.getCurrentPositionAsync({ accuracy: Location.Accuracy.High });
  this.setState({
    userLat: location.coords.latitude,
    userLng: location.coords.longitude,
    navGeoStatus: true,
    lat: location.coords.latitude,
    lng: location.coords.longitude
  });
} else {
        alert("Enable Location services"); //or any code to handle if location service is disabled otherwise
      }
Run Code Online (Sandbox Code Playgroud)

  • 这并不能解决问题。尽管已启用位置服务,但位置提供程序不可用。Location.getCurrentPositionAsync({ }) 未解析为 LocationObject 类型的对象。我在 Expo 的 GitHub 存储库上提出了一个问题。 (2认同)