我一直在玩 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 秒)(还有一个 demo,您需要打开控制台)
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');
}
}
最佳答案
如果您的应用需要如此精确的地理定位,那么也许您可以确定准确度属性的临界阈值是多少,并设置一个监听器来观察准确度的变化,并且仅在响应足够精确时才使用react。
关于javascript - 从 JavaScript 中高精度检索地理位置的最佳模式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17433178/