GPS地理定位坐标卡在PWA js中
我在 PWA 中使用地理定位时面临着 GPS 坐标卡住的问题。 问题是有时坐标会停留在之前的位置。例如我得到了一个地方的位置,该用户是一小时前的。
我相信在这种情况下我得到了 GPS 可以跟踪的最后一个位置的位置。
有没有办法刷新 GPS,并且只有在 GPS 具有有效的刷新连接后才发送数据?
地理定位代码:
if ("geolocation" in navigator && navigator.geolocation != null) {
try {
navigator.geolocation.getCurrentPosition(position => {
//if geolocation has valid connection
console.log(position);
$('#geolocation_form_latitude').val(position.coords.latitude);
$('#geolocation_form_longitude').val(position.coords.longitude);
$('#geolocation_form').submit();
}, error => {
$('#geolocation_form').submit();
})
}
catch (e) {
$('#geolocation_form').submit();
}
}
I am facing an issue with stucked gps coordinates using geolocation in a pwa.
The problem is that sometimes, the coordinates stucked in the previous location. For example I got the location of a place, the user was an hour ago.
I believe this cases I got the location of the last location the gps could track.
Is there anyway to refresh the gps, and only send the data once gps has valid, refreshed connection?
The code for geolocation:
if ("geolocation" in navigator && navigator.geolocation != null) {
try {
navigator.geolocation.getCurrentPosition(position => {
//if geolocation has valid connection
console.log(position);
$('#geolocation_form_latitude').val(position.coords.latitude);
$('#geolocation_form_longitude').val(position.coords.longitude);
$('#geolocation_form').submit();
}, error => {
$('#geolocation_form').submit();
})
}
catch (e) {
$('#geolocation_form').submit();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
移动操作系统通常会进行这种电池管理缓存 - 它减少了对 GPS 和 WiFi 等能源密集型无线电供电的需要。因此,大多数移动浏览器也会表现出这种陈旧的位置行为,因为它们依赖于 操作系统的位置服务。
尝试使用 中的
选项
HTML5 地理定位 API 查看它是否有助于获得更好的修复。另请注意,iOS 上的 Safari 和 Chrome 都会在返回的地理位置对象上返回其他属性,如此 Chrome 开发者文档 - 加载此测试页面,可用于验证修复的新鲜度。Mobile OSes routinely do this kind of cacheing for battery management - it reduces the need to power up energy-intensive radios such as GPS and WiFi. Hence most mobile browsers will also demonstrate this stale location behaviour because they are dependent on the OS's location services.
Attempt to use the
options
in the HTML5 geolocation API to see if it helps get a better fix. Also note that both Safari and Chrome on iOS return additional properties on the returned geolocation object as suggested in this Chrome developer documentation - I notice a key called "Cache Age" when loading this test page which may be used to verify freshness of the fix.