【问题标题】:react native geolocation for android returning cached position为返回缓存位置的android反应本机地理定位
【发布时间】:2020-08-01 11:24:24
【问题描述】:

我使用的是来自“react-native-geolocation-service”库的地理定位,详细信息如下。我的移动应用程序严重依赖位置(地理坐标),因此必须正确获取纬度/经度。我面临的问题是android正在返回缓存的位置。我已经在我的代码中删除了 maximumAge 参数,但我仍然在 android 中获取缓存的 lat long。有没有一种方法可以让我始终从 android 中的 GPS 获取当前位置,以使用这个库做出反应?任何帮助表示赞赏。谢谢。

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

【问题讨论】:

    标签: android react-native geolocation


    【解决方案1】:

    如果您不提供任何参数,那么它将使用该参数的默认值。因此,在这种情况下,您删除了maximumAge 参数,根据this doc,默认值为INFINITY

    所以,尝试在选项中传递maximumAge: 0,它会提供这样的新坐标:

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

    【讨论】:

      猜你喜欢
      • 2017-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多