【问题标题】:navigator.geolocation:: Location request timed out issue React Nativenavigator.geolocation:: Location request timed out issue React Native
【发布时间】:2019-06-17 23:02:51
【问题描述】:

我正在构建一个 React Native 应用程序并尝试使用 navigator.geolocation 获取我当前的地理编码它向我显示位置请求超时错误。基本上我正在构建一个跟踪应用程序,我需要配置确切的位置纬度经度。要开始跟踪,有一个按钮,单击此跟踪将开始,它应该是跟踪的初始点。我正在使用默认反应原生地理定位 API,链接 https://facebook.github.io/react-native/docs/geolocation 获取我当前位置的函数::

getLocation() {
    navigator.geolocation.getCurrentPosition(response => {
        console.log(response);
        if (response && response.coords) {
            this.saveLogs(response.coords);
        }
    },
        (error) => {
            this.onGeolocationErrorOccurCallback(error);
        },
        GeolocationOptions
    )
}

它显示一个错误::

{"TIMEOUT":3,"POSITION_UNAVAILABLE":2,"PERMISSION_DENIED":1,"message":"位置 请求超时", "code":3}

我添加了 Android 设备的权限,并通过将 enableHighAccuracy 值设置为 false 进行检查。但这对我不起作用。我需要将 enableHighAccuracy 设置为 true,因为我想要精确的地理编码来跟踪某人。请建议为什么在这里发生位置请求超时??

【问题讨论】:

    标签: react-native geolocation


    【解决方案1】:

    更新:2021 年 6 月 12 日,

    这种方法在 Android 模拟器中适用于我。 "react-native": "0.64.1"Android API 30

    import Geolocation from "@react-native-community/geolocation";
    
    const getGeoLocaation = () => {
      const config = {
        enableHighAccuracy: true,
        timeout: 2000,
        maximumAge: 3600000,
      };
    
      Geolocation.getCurrentPosition(
        info => console.log("INFO", info),
        error => console.log("ERROR", error),
        config,
      );
    };
    

    【讨论】:

    • 你已经解决了我的问题,谢谢!
    【解决方案2】:

    尝试使用:

    启用高精度:假

    为我工作!

        const getUserCurrentLocation = () => {
        let latitude, longitude
    
        Geolocation.getCurrentPosition(
            info => {
                const { coords } = info
    
                latitude = coords.latitude
                longitude = coords.longitude
    
                setLat(latitude)
                setLng(longitude)
    
                getUserCurrentAddress(latitude, longitude)
            },
            error => console.log(error),
            {
                enableHighAccuracy: false,
                timeout: 2000,
                maximumAge: 3600000
            }
        )
    }
    

    【讨论】:

      【解决方案3】:

      下面的代码对我有用:

      componentDidMount(){
         if(Platform.OS === 'android'){
            this.requestLocationPermission()
         }else{
            this._getCurrentLocation()
         }
      }
      
      async requestLocationPermission() {
          try {
              const granted = await PermissionsAndroid.request(
              PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
              {
                'title': 'Location Permission',
                'message': 'MyMapApp needs access to your location'
              }
              )
      
             if (granted === PermissionsAndroid.RESULTS.GRANTED) {
                 this._getCurrentLocation()
                 console.log("Location permission granted")
             } else {
                 console.log("Location permission denied")
             }
          } catch (err) {
             console.warn(err)
          }
      }
      
      _getCurrentLocation = () =>{
          navigator.geolocation.getCurrentPosition(
             (position) => {
             this.setState({
                latitude: position.coords.latitude,
                longitude: position.coords.longitude, 
             })
          },
          (error) => {
              this.setState({ error: error.message })},
              { enableHighAccuracy: true, timeout: 200000, maximumAge: 1000 },
          );
      }
      

      Manifest 文件中的权限:

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

      只需确保您的位置已启用。

      【讨论】:

      • 我已经尝试过了,但它不适用于 Redmi note 4 |安卓 7、MIUI 9 |
      • 它可以在任何其他设备上运行吗? @HarleenKaurArora
      • 只有 moto E-4 plus 但我需要在所有设备上工作。
      • 它在 ios 设备上运行,但大多数 Android 设备都存在问题。而且我已经设置了权限并在授予权限时调用了上述函数
      • 你能告诉我如何在 Android 设备上解决这个问题
      猜你喜欢
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      • 2022-12-28
      • 2023-02-09
      • 2012-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多