加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react-native – 响应本机navigator.geolocation.getCurrentPosi

发布时间:2020-12-15 16:15:13 所属栏目:百科 来源:网络整理
导读:我正在使用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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读