等待使用 getCurrentPosition 达到准确度阈值

发布于 2024-11-14 12:42:24 字数 411 浏览 2 评论 0原文

查看网络 GeoLocation API 文档 有两种获取位置的方法 - getCurrentPosition< /code> 快速读取位置,watchPosition 监视位置变化。

我正在寻找一种尽快获得非常准确的位置读数的方法。我认为理想的情况是在 getCurrentPosition 调用上使用准确度阈值 - 当达到阈值或超过超时时将调用成功处理程序(结果尽可能准确)。

这还不存在,对吧?想必实现这一点相当简单,只需包装 watchPosition 方法以在超时或达到阈值后停止检查。如果没有人知道执行此操作的内置方法,我将使用这种方法(并打算发布代码)...

Looking at the web GeoLocation API documentation there are two ways to get location - getCurrentPosition to get a quick reading of location and watchPosition to monitor the changes to the position.

What I'm looking for is a way to get a location reading which is quite accurate, as quickly as possible. I think the ideal would be to use an accuracy threshold on the getCurrentPosition call - and the success handler would be called when the threshold is reached or the timeout exceeded (with as accurate as possible a result).

This doesn't already exist, right? Presumably it would be fairly straightforward to implement this by just wrapping the watchPosition method to stop checking after a timeout or the threshold is reached. I will use this approach (and intend to post the code) if no-one is aware of a built in method to do this...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

素罗衫 2024-11-21 12:42:24

没有内置功能。我发现超时比设定的精度更好。通常,iPhone 上的精度不会超过 100 米左右,因此继续检查是否有更好的结果是浪费的。使用 watchPosition() 效果最好,而且它似乎可以进行三角测量,因此在读取三次之后,它很少会变得更好。我只等了五秒钟就停下来。

演示:http://jsfiddle.net/ThinkingStiff/3k859/

HTML:

<div id="result"></div>

脚本:

function setGeolocation() {

    var geolocation = window.navigator.geolocation.watchPosition( function ( position ) {

            document.getElementById( 'result' ).innerHTML += 
                  'lat: ' + position.coords.latitude + ', '
                + 'lng: ' + position.coords.longitude + ', '
                + 'accuracy: ' + position.coords.accuracy + '<br />';

        },
        function () {
            //error
        },
        {
            maximumAge: 250, 
            enableHighAccuracy: true
        } 

    );

    window.setTimeout( function () {

        window.navigator.geolocation.clearWatch( geolocation ) 

    }, 5000 );

};

setGeolocation();

No built in function. I've found that a timeout is better than a set accuracy. Often times on an iPhone the accuracy will be no better than around 100 meters, so continuing to check for better is wasteful. Using watchPosition() has worked best and it seems to triangulate, so after three readings it rarely gets better. I just wait five seconds and stop.

Demo: http://jsfiddle.net/ThinkingStiff/3k859/

HTML:

<div id="result"></div>

Script:

function setGeolocation() {

    var geolocation = window.navigator.geolocation.watchPosition( function ( position ) {

            document.getElementById( 'result' ).innerHTML += 
                  'lat: ' + position.coords.latitude + ', '
                + 'lng: ' + position.coords.longitude + ', '
                + 'accuracy: ' + position.coords.accuracy + '<br />';

        },
        function () {
            //error
        },
        {
            maximumAge: 250, 
            enableHighAccuracy: true
        } 

    );

    window.setTimeout( function () {

        window.navigator.geolocation.clearWatch( geolocation ) 

    }, 5000 );

};

setGeolocation();
雪化雨蝶 2024-11-21 12:42:24

根据我为出租车开发基于 J2ME 地理位置的应用程序的经验,GPS 芯片需要进行一些初始化才能获得准确的位置读数。首选方法是使用类似于 watchPosition() 的方法,这将使 GPS 芯片保持高温以提供准确的位置读取。

我认为你的方法在理论上是合理的,但需要在真实设备上进行测试,因为 GPS 芯片在不同设备上的性能有所不同。

希望这有帮助。

Based on my experience developing J2ME geo-location based apps for Taxi, GPS chip take some initialization to get accurate location reading. The preferred method is to use method similar to watchPosition(), which will keep the GPS chip hot to provide accurate location reading.

I think your approach is sound in theory, but it needs to be tested on real device as GPS chips varied in performance across devices.

Hope this helps.

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