简体   繁体   English

对 android 的本地地理定位做出反应,返回缓存的 position

[英]react native geolocation for android returning cached position

I was using Geolocation from 'react-native-geolocation-service' library and the details are as follows.我正在使用“react-native-geolocation-service”库中的地理定位,详细信息如下。 My mobile app is heavily depended on location (geo coordinates) hence getting the lat/lon correctly is a must.我的移动应用程序严重依赖位置(地理坐标),因此必须正确获取纬度/经度。 The problem I am facing is that android is returning the cached position.我面临的问题是 android 正在返回缓存的 position。 I have removed the maximumAge parameter in my code but still I am getting the cached lat long in android.我已经在我的代码中删除了 maximumAge 参数,但我仍然在 android 中获得缓存的 lat long。 Is there a way that I can always get the current location from the GPS in android in react native using this library?有没有一种方法可以让我始终从 android 中的 GPS 获取当前位置,以使用这个库进行本机反应? Any help is appreciated.任何帮助表示赞赏。 Thanks.谢谢。

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


Location.js:
import Geolocation from 'react-native-geolocation-service';

  getCoordinates = () => {
    Geolocation.getCurrentPosition(
      position => {
        this.setState({
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
        });
      },
      error => {
      }, { enableHighAccuracy: true, timeout: 15000 },
    )
  };

If you don't provide any parameter then it will use the default value for that parameter.如果您不提供任何参数,那么它将使用该参数的默认值。 So, In this case, you removed maximumAge parameter, which default value is INFINITY as per this doc .因此,在这种情况下,您删除maximumAge参数,根据此文档,默认值为INFINITY

So, try passing maximumAge: 0 in option and it will provide newly cordinates like this:因此,尝试在选项中传递maximumAge: 0 ,它将提供新的坐标,如下所示:

getCoordinates = () => {
    Geolocation.getCurrentPosition(
      position => {
        this.setState({
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
        });
      },
      error => {
      }, { enableHighAccuracy: true, timeout: 15000, maximumAge: 0 },
    )
  };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM