javascript - 从 JavaScript 中高精度检索地理位置的最佳模式是什么?

标签 javascript w3c-geolocation

我一直在玩 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/

相关文章:

javascript - JavaScript :void mean? 是什么意思

javascript - 将图像放在 Canvas JS 上

c# - 如何在 C# 应用程序中使用 HTML5 地理定位

添加回调函数时 JavaScript 停止工作

javascript - blob 因任何原因不存储任何值

javascript - jQuery 滑动切换和淡入问题

javascript - PHP 将字符串转换为变量,检查它的值是否不为空,与其他值进行比较

javascript - 打开和关闭 GPS 时的地理定位问题

javascript - Jquery ajax将变量发布到php

ios - Chrome IOS HTML5 地理定位权限被拒绝