【问题标题】:React Native Android location request timed outReact Native Android 位置请求超时
【发布时间】:2017-04-28 23:39:17
【问题描述】:

在 IOS 中查找 gps 坐标时没有问题。它工作正常。

在 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 }
    );
}

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

谢谢

【问题讨论】:

  • 我读过这篇文章,对此没有有效的解决方案。也感谢您的关注
  • 你有没有找到任何可行的解决方案。我面临同样的问题。我在任何地方都没有找到解决方案。
  • 我解决了这个问题,这是我使用 promise 创建的组件:gist.github.com/a3diti/5466f7309752cbb2e63ecefff6365bb9 在 android 中我删除了 maximumAge,我将 enableHighAccuracy 保持为 true,并从 RN PermissionsAndroid 组件添加了权限检查。跨度>

标签: android react-native gps react-native-android


【解决方案1】:

我通过使用本机 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.
    ...
}

最后别忘了在Android SDK Manager中下载Google Play ServicesGoogle Support Library,您可以在其中找到您的播放服务版本;

<android-sdk-location>/<sdk-version>/extras/google/m2repository/com/google/android/gms/play-services

【讨论】:

    【解决方案2】:

    您可能在建筑物或办公室内,并且您的信号不是那么好。我尝试了很多配置,这一款最适合我:

    {
        enableHighAccuracy: false,
        timeout: 5000,
        maximumAge: 10000
    }
    

    或者尝试增加超时。因为对我来说,通常的 2000 毫秒不起作用,所以我不断收到“位置请求超时”。所以禁用高精度,增加超时时间就可以了。

    【讨论】:

      【解决方案3】:

      诀窍是,如果你设置了

      { enableHighAccuracy: true, timeout: 25000, maximumAge: 3600000 } 
      

      那么它就会起作用。完整代码如下。

      import React, { Component } from 'react';
      import { View, Text } from 'react-native';
      
      class GeolocationExample extends Component {
      constructor(props) {
      super(props);
      
      this.state = {
        latitude: null,
        longitude: null,
        error: null,
      };
      }
      
      componentDidMount() {
      navigator.geolocation.getCurrentPosition(
        (position) => {
          this.setState({
            latitude: position.coords.latitude,
            longitude: position.coords.longitude,
            error: null,
             });
             },
        (error) => this.setState({ error: error.message }),
        { enableHighAccuracy: true, timeout: 25000, maximumAge: 3600000 },
      );}
      
       render() {
      return (
        <View style={{ flexGrow: 1, alignItems: 'center', justifyContent: 'center' }}>
          <Text>Latitude: {this.state.latitude}</Text>
          <Text>Longitude: {this.state.longitude}</Text>
          {this.state.error ? <Text>Error: {this.state.error}</Text> : null}
        </View>
        );
       }
      }
      
      export default GeolocationExample;
      

      【讨论】:

      • 您认为它将如何提供正确的当前位置而不是缓存中的内容?
      • 问题与请求超时有关,因此如果您将 maximumAge & timeout 设置为更高的数字,它可能无法获取实时数据,而是从缓存中获取
      【解决方案4】:

      我在 Android 上删除了 maximumAge: 3600000 并且它可以正常工作

      【讨论】:

      • Rremove maximumAge 应被接受以禁用 Android 上的“位置请求超时”警报。
      • 这适用于android,应该是公认的答案,谢谢
      • 没有获得确切位置的任何人都可以面临这个问题吗?
      【解决方案5】:

      我已按位置请求完成了该应用程序。命令
      { enableHighAccuracy: true, timeout: 25000, maximumAge: 3600000 }, 可以在 iOS 上运行,但不能在 Android 上运行。我认为你应该检查平台。

      navigator.geolocation.getCurrentPosition(
                (location) => {
                  console.log('location ', location);
                  if (this.validLocation(location.coords)) {
                    this.locationToAddress(location.coords);
                  }
                },
                (error) => {
                  console.log('request location error', error);
                },
                Platform.OS === 'android' ? {} : { enableHighAccuracy: true, timeout: 20000, maximumAge: 10000  }
              );
      

      【讨论】:

        【解决方案6】:

        我尝试了各种方法和 npm 模块。最后我通过使用这个模块解决了这个问题。我强烈建议这样做。 https://www.npmjs.com/package/react-native-geolocation-service

        【讨论】:

          【解决方案7】:

          工作 { enableHighAccuracy: false, timeout: 5000, maximumAge: 10000 },

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

          在 P 列表和 android Manifest 中添加权限

          import React, { Component } from 'react';
          import { View, Text } from 'react-native';
          
          export default class App extends Component {
            constructor(props) {
              super(props);
          
              this.state = {
                latitude: null,
                longitude: null,
                error: null,
              };
            }
          
            componentDidMount() {
              navigator.geolocation.getCurrentPosition(
                (position) => {
                  this.setState({
                    latitude: position.coords.latitude,
                    longitude: position.coords.longitude,
                    error: null,
                  });
                },
                (error) => this.setState({ error: error.message }),
                { enableHighAccuracy: false, timeout: 5000, maximumAge: 10000 },
                // { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 },
              );
            }
          
            render() {
              return (
                <View style={{ flexGrow: 1, alignItems: 'center', justifyContent: 'center' }}>
                  <Text>Latitude: {this.state.latitude}</Text>
                  <Text>Longitude: {this.state.longitude}</Text>
                  {this.state.error ? <Text>Error: {this.state.error}</Text> : null}
                </View>
              );
            }
          }
          

          【讨论】:

            【解决方案8】:

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

            【讨论】:

              【解决方案9】:
              import Geolocation from '@react-native-community/geolocation';
              
              
              Geolocation.getCurrentPosition(
                    position => {
                      const initialPosition = JSON.stringify(position);
                      console.log(initialPosition);
                    },
                    error => Alert.alert('Error', JSON.stringify(error)),
                    {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000},
                  );
              
              

              这个功能对我有用,我已经用

              更改了“AndroidManifest.xml”文件
              <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
              

              【讨论】:

              • 这行得通。一些低端设备需要时间才能获得当前位置。所以最好增加超时时间
              【解决方案10】:

              在我的情况下,删除超时和maximumAge 并将enableHighAccuracy 设置为false 后它工作正常。

              【讨论】:

                【解决方案11】:

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

                支持的选项:

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

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

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

                Detailed Documentation

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

                【讨论】:

                • 非常感谢!
                【解决方案12】:

                删除参数“maximumAge”并保留

                { enableHighAccuracy: true, timeout: 2000}
                

                【讨论】:

                  【解决方案13】:

                  我建议考虑两种处理方式:

                  1. 使用react-native-geolocation-service - 一个可以解决该问题的软件包。
                  2. 尝试使用 enableHighAccuracy=true 调用一次,如果失败,请尝试使用 enableHighAccuracy=false 再次调用它。
                    详细信息:似乎 enableHighAccuracy=true 尝试通过 GPS 获取位置 - 当 enableHighAccuracy=false 时,设备首先尝试通过 Wifi 获取位置,如果失败则尝试通过 GPS 获取位置。

                  这是我实现第 2 项的方法:

                  import Geolocation from "@react-native-community/geolocation";
                  
                  const getCurrentLocation = (resolve, enableHighAccuracy, onError) => {
                      console.log(`[getCurrentPosition] enableHighAccuracy=${enableHighAccuracy.toString()}`);
                  
                      Geolocation.getCurrentPosition(
                          (position) => {
                              const location = {
                                  latitude: position.coords.latitude,
                                  longitude: position.coords.longitude
                              }
                              console.log('[getCurrentPosition] success');
                              resolve(location);
                          },
                          onError,
                          {enableHighAccuracy, timeout: 20000}
                      );
                  };
                  
                  /**
                   * We first try to get location with enableHighAccuracy=true (try to get position via GPS) if it fails try to get it with enableHighAccuracy=false (try to get position from wifi)
                   * @return {Promise}
                   */
                  export const getCurrentPosition = () => {
                      return new Promise((resolve) => {
                          getCurrentLocation(resolve, true,
                              (error1) => {
                                  // failed to retrieve with enableHighAccuracy=true - now try with enableHighAccuracy=false
                                  console.log(`[getCurrentPosition] failed 1st time trying again with enableHighAccuracy=false, error: ${error1.message}`);
                  
                                  getCurrentLocation(resolve, false, (error2) => {
                                      // Could not retrieve location - we can live with that
                                      console.log(`[getCurrentPosition] failed ${error2.message}`);
                                      resolve();
                                  })
                              });
                      });
                  }
                  

                  【讨论】:

                    【解决方案14】:

                    我被困了 3 天,但那一切都是徒劳的 我只是将超时更改为 20000 毫秒。 现在问题解决了。

                    GetLocation.getCurrentPosition({ 启用高精度:真, 超时:20000,

                    只需更改/增加“超时”值即可解决您的问题。

                    【讨论】:

                      猜你喜欢
                      • 2016-05-19
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2018-01-31
                      • 1970-01-01
                      • 2021-04-07
                      • 2020-04-27
                      • 1970-01-01
                      相关资源
                      最近更新 更多