react-native – 响应本机navigator.geolocation.getCurrentPosition不更新

我正在使用navigator.geolocation.getCurrentPosition()来获取移动应用中的当前位置.

首先,它正确地获得当前位置.

我每隔一秒调用一次函数来保持位置值(纬度,经度)的更新.

但是每次调用函数时,它都返回相同的值,时间戳也没有变化.

有任何想法吗?

最佳答案 您应该考虑使用参数’maximumAge’并将其设置为0以避免获取缓存值.例如

navigator.geolocation.getCurrentPosition(position => {
      region: {
        latitude: position.coords.latitude,
        longitude: position.coords.longitude
      }
}, err => {
  console.log(err)
  alert('fetching the position failed')
}, {enableHighAccuracy: false, timeout: 20000, maximumAge: 0})

按照MDN documentation

The PositionOptions.maximumAge property is a positive long value indicating the maximum age in milliseconds of a possible cached position that is acceptable to return. If set to 0, it means that the device cannot use a cached position and must attempt to retrieve the real current position. If set to Infinity the device must return a cached position regardless of its age.

点赞