查看网络 GeoLocation API documentation有两种获取位置的方法 - getCurrentPosition
可以快速读取位置,watchPosition
可以监控位置的变化。
我正在寻找一种方法来获取位置读数,该读数非常准确,而且要尽可能快。我认为理想的做法是在 getCurrentPosition
调用上使用精度阈值 - 并且在达到阈值或超过超时时调用成功处理程序(结果尽可能准确)。
这还不存在,对吧?大概通过包装 watchPosition
方法以在超时或达到阈值后停止检查来实现这一点是相当简单的。如果没有人知道执行此操作的内置方法,我将使用这种方法(并打算发布代码)...
最佳答案
没有内置函数。我发现超时比设置精度更好。在 iPhone 上,精度通常不会超过 100 米左右,因此继续检查以提高精度是一种浪费。使用 watchPosition()
效果最好,而且它似乎可以进行三 Angular 测量,因此在读取三个读数后它很少会好转。我只等了五秒钟就停下了。
演示: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();
关于javascript - 等待使用 getCurrentPosition 达到准确度阈值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6264985/