GPS地理定位坐标卡在PWA js中

发布于 2025-01-09 03:54:42 字数 969 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

錯遇了你 2025-01-16 03:54:42

移动操作系统通常会进行这种电池管理缓存 - 它减少了对 GPS 和 WiFi 等能源密集型无线电供电的需要。因此,大多数移动浏览器也会表现出这种陈旧的位置行为,因为它们依赖于 操作系统的位置服务

尝试使用 中的选项 HTML5 地理定位 API 查看它是否有助于获得更好的修复。另请注意,iOS 上的 Safari 和 Chrome 都会在返回的地理位置对象上返回其他属性,如此 Chrome 开发者文档 - 加载此测试页面,可用于验证修复的新鲜度。

最大年龄
是一个长整型正值,指示可接受返回的可能缓存位置的最长期限(以毫秒为单位)。如果设置为 0,则意味着设备无法使用缓存的位置,并且必须尝试检索真实的当前位置。如果设置为“Infinity”,则设备必须返回缓存的位置,无论其年龄如何。默认值:0。

启用高精度
是一个布尔值,表示应用程序希望收到最好的结果。如果属实,并且设备能够提供更准确的位置,它就会这样做。请注意,这可能会导致响应时间变慢或功耗增加(例如移动设备上的 GPS 芯片)。另一方面,如果为假,则设备可以通过更快地响应和/或使用更少的电量来自由地节省资源。默认值:假。

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.

maximumAge
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. Default: 0.

enableHighAccuracy
Is a boolean value that indicates the application would like to receive the best possible results. If true and if the device is able to provide a more accurate position, it will do so. Note that this can result in slower response times or increased power consumption (with a GPS chip on a mobile device for example). On the other hand, if false, the device can take the liberty to save resources by responding more quickly and/or using less power. Default: false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文