本机反应无法从蜂窝网络找到位置

react native can't find location from cellular network

我正在尝试在 android 设备 (Android 7.0) 中以 react-native 获取用户的经纬度位置。当我打开 gps 时,它可以毫无问题地找到位置,但是如果我想从 wifi 或地窖网络获取位置并且 gps 关闭,它会给我错误 "no location provider available"。

我在我的 android 清单中拥有此权限并在应用程序中动态检查它们:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

根据我的理解,enableHighAccuracy: true 提供了来自 gps 的位置,而 enableHighAccuracy: false 应该提供了来自 wifi 或 cellar 网络的位置,但第二个无法正常工作。

这是我的代码:

    let highAccuracySuccess = false;
    let highAccuracyError = false;
    let highAccuracy = true;
    let timeout = 30000;

    let getLowAccuracyPosition = () => {
      console.log("REQUESTING POSITION", "HIGH ACCURACY FALSE");
      navigator.geolocation.watchPosition(
        position => {
          console.log("POSITION NETWORK OK", position);
          this.setState({
            latitude: position.coords.latitude,
            longitude: position.coords.longitude,
            error: null
          });
        },
        error => {
          console.log(error);
          this.setState({
            error: "error2" + error.message
          });
        },
        {
          enableHighAccuracy: false,
          timeout: 30000,
          maxAge: 0
        }
      );
    };

    if (highAccuracy) {
      console.log("REQUESTING POSITION", "HIGH ACCURACY TRUE");
      const watchId = navigator.geolocation.watchPosition(
        position => {
          // location retrieved
          highAccuracySuccess = true;
          console.log("POSITION GPS OK", position);
          navigator.geolocation.clearWatch(watchId);
          this.setState({
            latitude: position.coords.latitude,
            longitude: position.coords.longitude,
            error: null
          });
        },
        error => {
          console.log(error);
          this.setState({
            error: error.message
          });
          highAccuracyError = true;
          navigator.geolocation.clearWatch(watchId);
          getLowAccuracyPosition();
        },
        {
          enableHighAccuracy: true,
          timeout: 20000,
          maxAge: 0,
          distanceFilter: 1
        }
      );

      setTimeout(() => {
        if (!highAccuracySuccess && !highAccuracyError) {
          getLowAccuracyPosition();
        }
      }, timeout);
    }

根据GoogleAndroid支持,

If you turn off Location for your device, then no apps can use your device location

因此应打开定位服务以确定设备位置。

关于使用 enableHighAccuracy: true or false

再次来自GoogleAndroid支持,

位置服务有三种模式,设备所有者可以选择其中的任何一种。

High accuracy This mode uses GPS, Wi-Fi, mobile networks, and sensors to get the highest-accuracy location. It uses Google's Location services to help estimate your device's location faster and more accurately.

Battery saving This mode uses sources that use less battery, like Wi-Fi and mobile networks. It uses Google's Location services to help estimate your device's location faster and more accurately.

Device only This mode uses only GPS. It doesn’t use Google's Location services to provide location information. It can estimate your device's location slower and use more battery.

上述模式是在 Android API 23(最有可能)中添加的,因此在 API <= 21 你的 enableHighAccuracy 属性 应该已经决定了 Location 的类型使用的服务模式。在 API >= 23 上,用户选择的模式将决定你获得的准确度。

在 iOS 上,行为也是相同的,除了用户不会像 Android.

中那样有明确的模式选项。

TL, DR: 无论您使用 Wifi、网络,都应打开位置服务以确定当前位置(iOS 或 Android)或 GPS 系统。

一些有用的链接:

https://support.google.com/accounts/answer/3467281?hl=en

https://github.com/expo/expo/issues/1339