我一直在使用 JavaScript Geolocation API(主要在 iOS 6 和 Android Jellybean 设备中),尽管通过了:
{
enableHighAccuracy: true,
maximumAge: 0
}
作为 PositionOptions,模式似乎是在第一个响应中我得到一个快速的低精度响应(该location.coords.accuracy
值高达 16,130),在第二个或第三个响应中我得到一个高精度响应(location.coords.accuracy
值低至31)。现在我正在实现一个 setTimeout 调用(延迟 3 秒)来查询 GPS 两次,我忽略了第一个响应。但我想知道是否有人对更好的实现有任何指示。这是我的代码(有 3 秒超时)(还有一个演示,你需要打开控制台)
var check = function() {
if ("geolocation" in navigator) {
console.log('looking');
navigator.geolocation.getCurrentPosition(function(location) {
console.log('Ignoring the following data:');
console.log({
lat: location.coords.latitude,
lng: location.coords.longitude,
accuracy: location.coords.accuracy});
setTimeout(function() {
navigator.geolocation.getCurrentPosition(function(location) {
console.log('On my system I am firing a Lucene Spatial search with the following data:');
console.log({
lat: location.coords.latitude,
lng: location.coords.longitude,
accuracy: location.coords.accuracy});
}, function() {
console.log('navigator error occurred on second call... weird');
}, {
enableHighAccuracy: true,
maximumAge: 0
});
}, 3000);
}, function() {
console.log('navigator error occurred on first call.');
}, {
enableHighAccuracy: true,
maximumAge: 0
});
} else {
console.log('geolocation not available');
}
}