【问题标题】:navigator.geolocation.getCurrentPosition returns "Unknown error acquiring position"navigator.geolocation.getCurrentPosition 返回“未知错误获取位置”
【发布时间】:2018-12-29 20:16:20
【问题描述】:

我目前正在使用地理定位 API。

当允许我的浏览器 - localhost 或 webhost- 获取我的位置时,我的 console.log 返回:

获取位置未知错误

我无法弄清楚,因为我已经授权了连接,而且我的代码似乎很干净,因为我是从 Mozilla 官方 MDN 获取的。

这里是我的 client.js:

      var options = {
            enableHighAccuracy: true,
            timeout: 5000,
            maximumAge: 0
      };

      function success(pos) {
        var crd = pos.coords;

        console.log('Votre position actuelle est :');
        console.log(`Latitude : ${crd.latitude}`);
        console.log(`Longitude: ${crd.longitude}`);
        console.log(`Plus ou moins ${crd.accuracy} mètres.`);
      };

      function error(err) {
        console.warn(`ERROR(${err.code}): ${err.message}`);
      };

      navigator.geolocation.getCurrentPosition(success, error, options);

任何提示都会很棒, 谢谢

【问题讨论】:

    标签: javascript node.js geolocation


    【解决方案1】:

    Google 已更改(5 月至 6 月)有关 API 密钥的一些政策(现在需要有效的 API 密钥)。如果您转到 Firefox 并输入 about:config 并搜索属性 geo.wifi.uri,您将看到没有 api 密钥的 google url。您可以将 url 更改为另一个地理服务 (https://location.services.mozilla.com/v1/geolocate?key=test),但对于生产,最好执行直接 ajax 调用,如下所示:

        const res = await fetch('https://location.services.mozilla.com/v1/geolocate?key=test').then(el=>el.json())
        const point = [res.location.lat, res.location.lng]
    

    一旦 Firefox 解决了问题,您就可以像以前一样使用地理定位 API。

    (另见html geolocation: Unknown error acquiring position

    【讨论】:

      【解决方案2】:

      目前这是我用来获取用户纬度/经度的方法

      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
          var pos = {
            lat: position.coords.latitude,
            lng: position.coords.longitude
          };
          console.log("Lat: " + pos.lat + ". Lng: " + pos.lng);
        })
      }
      

      这使用浏览器的地理位置并返回用户的 lat 和 lng。然后我使用google的reversegeocoding来获取实际地址。

      【讨论】:

      • 谢谢,到目前为止它什么也没给我返回,比如“null”,没有日志,什么都没有
      • 你用的是什么浏览器?该浏览器是否允许共享用户位置?
      • 是的,我刚刚安装了 Chrome,它在 Chrome 上运行良好,但在其他浏览器上运行良好 - Firefox 61.0.1 _ Opera 54.0.2952.54 - 肯定是浏览器的问题
      • 我已经更精确地安装了 Google Chrome 67.0.3396.99。而且我正在使用以太网电缆连接,我认为在我的设备中未启用 wifi,我的硬件存在问题,并且 Ubuntu 16.04 似乎
      • 这应该不是问题,您是否尝试过检查 Firefox 是否关闭了隐私模式并授予网站用户位置。我刚刚在 windows 10 上的 Firefox 和 Edge 上进行了测试,这段代码就像一个魅力。 Firefox 确实有一点延迟,但没什么大不了的。
      猜你喜欢
      • 2017-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多