【问题标题】:What is an optimal pattern for retrieving a geolocation from JavaScript with high accuracy?从 JavaScript 高精度检索地理位置的最佳模式是什么?
【发布时间】:2013-06-30 06:26:56
【问题描述】:

我一直在使用 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');
  }
}

【问题讨论】:

  • 想展示您的实际代码吗?我不确定它有多重要,但它不会受到伤害
  • @Ian,我添加了代码和 JSFiddle 演示
  • 与其忽略第一个结果并保留第二个结果,为什么不假设您需要采集多个样本并安排 N 次检查,以保持最准确的一个?
  • 而不是 getCurrentPosition 使用 watchPosition 并捕获小于关键错误的点。 developer.mozilla.org/en-US/docs/Web/API/Geolocation/…

标签: javascript w3c-geolocation


【解决方案1】:

如果您的应用需要如此精确的地理位置,那么也许您可以确定准确度属性的临界阈值是多少,并设置一个监听器来观察准确度的变化,并仅在响应足够精确时做出反应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 2018-12-11
    相关资源
    最近更新 更多