React Native Android位置请求超时

İsm*_*ner 19 gps android react-native react-native-android

在IOS中,寻找gps coords时没有问题.它工作正常.

在Android方面,它不像IOS那样稳定.在真实设备和模拟器中都存在这个问题.有时它可以找到位置,但有时不会.寻找3天但没有找到解决方案.

当我的应用程序无法找到我的位置时,我尝试通过谷歌地图应用程序,它就像魅力.

这是我的IOS和Android代码;

getCurrentPosition() {
    navigator.geolocation.getCurrentPosition(
        (position) => {
            this.setState({ initialPosition: position });
            alert(JSON.stringify(position))
            var coords = new Coords();
            coords.Latitude = position.coords.latitude;
            coords.Longitude = position.coords.longitude;
            this.getPharmaciesByCoordinates(coords);
        },
        (error) => alert(error.message),
        { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
    );
    this.watchID = navigator.geolocation.watchPosition((position) => {
        this.setState({ initialPosition: position });
    },
        (error) => alert(error.message),
        { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
    );
}
Run Code Online (Sandbox Code Playgroud)

欢迎任何类型的解决方案.

谢谢

Tha*_* Ha 10

maximumAge: 3600000在Android上将其删除了


小智 8

import Geolocation from '@react-native-community/geolocation';

Run Code Online (Sandbox Code Playgroud)
Geolocation.getCurrentPosition(
      position => {
        const initialPosition = JSON.stringify(position);
        console.log(initialPosition);
      },
      error => Alert.alert('Error', JSON.stringify(error)),
      {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000},
    );

Run Code Online (Sandbox Code Playgroud)

此功能对我有用,我已将“AndroidManifest.xml”文件更改为

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Run Code Online (Sandbox Code Playgroud)


dex*_*on_ 6

您可能在建筑物或办公室内,但信号不太好。我尝试了多种配置,而这种配置最适合我:

{
    enableHighAccuracy: false,
    timeout: 5000,
    maximumAge: 10000
}
Run Code Online (Sandbox Code Playgroud)

或尝试增加超时时间。因为对我来说,通常的2000ms无效,所以我一直收到“位置请求超时”的信息。因此,禁用高精度,增加超时时间就可以了。


Sab*_*viş 6

对于那些仍然面临问题的人,请注意参数。我曾经将参数保留为默认值,但是当涉及到enableHighAccuracy时,你应该小心,因为当你将其设置为false时,这意味着你正在请求WIFI位置而不是GPS位置。如果您不将其设置为 true,您可能会像我一样在模拟器上收到错误。

支持的选项:

超时 (ms) -是一个正值,表示设备返回位置所允许的最大时间长度(以毫秒为单位)。默认为无限。

MaximumAge (ms) -是一个正值,指示可接受返回的可能缓存位置的最大期限(以毫秒为单位)。如果设置为 0,则意味着设备无法使用缓存的位置,并且必须尝试检索真实的当前位置。如果设置为“Infinity”,则设备将始终返回缓存的位置,无论其年龄如何。默认为无限。

enableHighAccuracy (bool) -是一个布尔值,表示是否使用 GPS。如果设置为 true,将请求 GPS 位置。如果设置为 false,将请求 WIFI 位置。

详细文档

编辑1; 我建议大家使用这个库,而不是本机库(react-native-geolocation-service),因为它是为此类超时问题而创建的。


Vis*_*nde 5

嗨,我遇到了同样的问题“请求超时”。我已经尝试了我找到的所有可能的解决方案,但是,那些对我不起作用,但最近我找到了一个克服了这个问题的模块。 react-native-geolocation-service 这对我有用.....


İsm*_*ner 1

我通过使用 native 的外部库找到了一个解决方案LocationManager。它使用播放服务位置并根据我的需要发出位置事件。

这里是图书馆; https://bitbucket.org/timhagn/react-native-google-locations#readme

当为 android 编译时,不要忘记包含android/app/build.gradle

dependencies {
    ...
    compile "com.google.android.gms:play-services:10.0.1" -> which version that you have write down here.
    ...
}
Run Code Online (Sandbox Code Playgroud)

最后不要忘记下载Google Play ServicesGoogle Support LibraryAndroid SDK Manager中您可以找到您的播放服务版本;

<android-sdk-location>/<sdk-version>/extras/google/m2repository/com/google/android/gms/play-services
Run Code Online (Sandbox Code Playgroud)