React-native应用程序的位置请求在后台(模拟器)时超时

时间:2017-11-21 06:17:53

标签: android react-native

我使用navigator.geolocation.getCurrentPosition在我的应用中获取坐标。

最初,我的应用程序允许通过以下方式在AndroidManifest.xml中指定位置服务:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

位置服务工作正常。然后,我转到模拟器的设置,将应用程序置于后台,关闭该位置,然后返回到检测到位置确实已关闭的应用程序,因此它会提示我并让我启用再次通过设置进行定位,使用以下代码:

if (Platform.OS === 'android') {
  LocationServicesDialogBox.checkLocationServicesIsEnabled({
    message: "<h2>Use Location ?</h2>",
    ok: 'YES',
    cancel: 'NO',
    enableHighAccuracy: false,
    showDialog: true,
    openLocationServices: true,
  }).then((success) => {
    console.log(success);
  }).catch((error) => {
    console.log(error.message);
  });
}

当我点击“确定”时如果我应该允许该应用获取位置权限,则会显示“#”位置请求已超时&#39;。这是我的getCurrentLocation代码:

handleRefreshLocation() {
  console.log('refreshing location');
  navigator.geolocation.getCurrentPosition(
    (position) => {
      console.log('location refreshed');
      this.setState({
        long: position.coords.longitude,
        lat: position.coords.latitude,
        error: null,
      }, this.updateLocation); //updateLocation is an API call to update DB
    },
    error => this.setState({ error: error.message }, this.printError),
    { enableHighAccuracy: false, timeout: 20000 },
  );
} 

我已经尝试将enableHighAccuracy设置为false,或者完全删除getCurrentPosition的第三个参数,但没有任何效果。还有其他解决办法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

您是否尝试仅删除enableHighAccuracy参数并保留超时?

还可以尝试将其添加到AndroidManifest.xml:

{{1}}