javascript - navigator.geolocation 报告错误的第一个值

标签 javascript ios mobile geolocation mobile-safari

我目前正在制作一个移动网络应用程序,该应用程序使用户可以使用该应用程序行走,并将测量所走的距离(仅限直线)。显然,使用 Haversine 很好,并且计算按预期进行。但是,我在 iOS 5(iPhone 3GS,未在 4 和 4G 上测试)上测试时注意到一个小错误。

这是我用来观察他们位置的 JavaScript:

var points = [ ]; // This will be an array holding all of the points we have recorded.

// Start watching the position:
navigator.geolocation.watchPosition(function(location) {
    var this_coords = { lat: location.coords.latitude, lng : location.coords.longitude };
    points.push(this_coords);
}, function() {  }, true);

// When we're done, I output them to the console:
var start = points[0],
end = points[points.length - 1];
console.log('Start: ' + start.lat + ', ' + start.lng);
console.log('End: ' + end.lat + ', ' + end.lng);

本质上,所有的点都被记录到一个数组中,然后第一个和最后一个纬度/经度对用于 Haversine。但是,输入到数组中的初始值似乎总是不正确(相距很远)。最终值总是正确的(在 ~ 5m 以内)。

有谁知道为什么第一次阅读可能如此不准确,或者我该如何避免这种情况?我想过也许忽略第一次,然后进行第二次阅读,但这显然不是很科学。

最佳答案

第一个位置响应最快,然后它会尝试进一步缩小范围。当它尝试通过电话连接、wifi 和 gps 进行连接时,它们都在不同的时间出现。

您可以在执行前先使用 position.coords.accuracy 缩小范围。

例如

 if (position.coords.accuracy < 100){
           // if accuracy is less than 100m do something

    }

关于javascript - navigator.geolocation 报告错误的第一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9555584/

相关文章:

javascript - JQuery 点击事件在 Thymeleaf 上不起作用

ios - 确认密码的正则表达式

java - uid 没有 android.permission.SEND_SMS

css - 响应式全宽部分

javascript - 限制也考虑数字的单词

javascript - 对象、它们的实例和 setTimeOut 效果

javascript - 过滤 firebase 数据并排序结果

objective-c - 在应用程序委托(delegate)内执行初始 View Controller 的序列

ios - 使用 Cocoapods 进行 Xcode 单元测试

html - 您推荐哪种 HTML5 移动框架?