为啥我的 PhoneGap 定位函数多次返回位置?

Posted

技术标签:

【中文标题】为啥我的 PhoneGap 定位函数多次返回位置?【英文标题】:Why does my PhoneGap location function return the position multiple times?为什么我的 PhoneGap 定位函数多次返回位置? 【发布时间】:2017-09-20 19:38:05 【问题描述】:

我有这个功能应该可以得到更准确的 GPS 结果:

navigator.geolocation.getAccurateCurrentPosition = function (geolocationSuccess, geolocationError, geoprogress, options) 
    var lastCheckedPosition,
        locationEventCount = 0,
        watchID,
        timerID;

    options = options || ;

    var checkLocation = function (position) 
        lastCheckedPosition = position;
        locationEventCount = locationEventCount + 1;
        // We ignore the first event unless it's the only one received because some devices seem to send a cached
        // location even when maxaimumAge is set to zero
        if ((position.coords.accuracy <= options.desiredAccuracy) && (locationEventCount > 1)) 
            clearTimeout(timerID);
            navigator.geolocation.clearWatch(watchID);
            foundPosition(position);
         else 
            geoprogress(position);
        
    ;

    var stopTrying = function () 
        navigator.geolocation.clearWatch(watchID);
        foundPosition(lastCheckedPosition);
    ;

    var onError = function (error) 
        clearTimeout(timerID);
        navigator.geolocation.clearWatch(watchID);
        geolocationError(error);
    ;

    var foundPosition = function (position) 
        geolocationSuccess(position);
    ;

    if (!options.maxWait)            options.maxWait = 10000; // Default 10 seconds
    if (!options.desiredAccuracy)    options.desiredAccuracy = 20; // Default 20 meters
    if (!options.timeout)            options.timeout = options.maxWait; // Default to maxWait

    options.maximumAge = 0; // Force current locations only
    options.enableHighAccuracy = true; // Force high accuracy (otherwise, why are you using this function?)

    watchID = navigator.geolocation.watchPosition(checkLocation, onError, options);
    timerID = setTimeout(stopTrying, options.maxWait); // Set a timeout that will abandon the location loop
;

然后我打电话给:

navigator.geolocation.getAccurateCurrentPosition(onSuccess, onError, onProgress, desiredAccuracy:20, maxWait:15000);

当它找到一个位置时,它会触发geolocationSuccess(position);(从上面的函数成功。问题是在我的PhoneGap应用程序中,它似乎经常调用它多次。有时在同一个中4-5次第二个。

为什么会这样?我只初始化一次,它没有在任何循环中。

【问题讨论】:

【参考方案1】:

脚本作者在github readme 上回答了您的问题(重点是我的):

更好的方法是使用 navigator.geolocation.watchPosition()。每次位置更改或每次设备提高准确性时(根据我的观察),此方法都会进行回调。在我自己使用新启动的设备进行的测试中,需要 2 到 6 次回调才能达到高度准确的结果。这促使我编写了这个非常简单的 javascript 函数将 watchPosition() 与一个简单的计时器结合使用。

所以作者在这个实现中使用了geolocation watchPosition()方法。 如果位置或精度发生变化,watchPosition() 方法的回调可能会被多次调用。见documentation(重点是我的):

如果位置数据发生变化(通过设备移动或更多 准确的地理信息到达),可以设置回调函数 使用该更新的位置信息调用。这个完成了 使用具有相同输入的 watchPosition() 函数 参数为 getCurrentPosition()。 回调函数被调用 多次,允许浏览器将您的位置更新为 您移动或提供更准确的位置作为不同的技术 用于对您进行地理定位。错误回调函数,即 与 getCurrentPosition() 一样是可选的,可以调用 反复。

【讨论】:

谢谢,这帮助我解决了问题。我在成功接收到一个位置后最终设置了一个变量,然后阻止了代码再次运行。

以上是关于为啥我的 PhoneGap 定位函数多次返回位置?的主要内容,如果未能解决你的问题,请参考以下文章

Phonegap 地理位置 PERMISSION_DENIED

PhoneGap - 地理位置错误

为啥当我遍历位置时地理定位不起作用

phonegap 地理定位不起作用

“原生”地理定位与“基于PhoneGap”的地理定位功能

为啥会重复请求didUpdateBMKUserLocation这个方法