【问题标题】:Geolocation permission地理位置许可
【发布时间】:2014-10-01 19:28:33
【问题描述】:

我正在尝试做一个简单的地理位置 html5:

$(function() {  
    var currentLatitude ="";
    var currentLongitude ="";
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition, showError);
    function showPosition(position) {
            currentLatitude = position.coords.latitude;
            currentLongitude = position.coords.longitude;                                                
        }   
    } else {
       // location based on the IP - less accurate

    }  

即使我的网站(在本地运行)出现异常,错误也是位置错误的许可。 我的第二个选择是通过 IP (ipinfo.io):

function showError(error) {
      var x = $('#curr_loc_target');

      switch(error.code) {
        case error.PERMISSION_DENIED:
          x.html("User denied the request for Geolocation.");
           jQuery.get("http://ipinfo.io/json", function (response)
               {
                    console.log(response) ;
                   currentLatitude = response.loc.split(',')[0]; 
                   currentLongitude = response.loc.split(',')[1];
                   console.log("lat" + currentLatitude+ "," + "lngs" +currentLongitude) ;
               } );
          break;
        case error.POSITION_UNAVAILABLE:
          x.html("Location information is unavailable.");
          break;
        case error.TIMEOUT:
          x.html("The request to get user location timed out.");
          break;
        case error.UNKNOWN_ERROR:
          x.html("An unknown error occurred.");
          break;
        }
    }

它有效,但它根本不准确。 我需要做的就是将 lat、lng 发送到我的服务器,如果它不是本地的,它会工作吗? 或者有人知道解决此错误的方法吗?

【问题讨论】:

  • 我真的不知道 ipinfo 有什么样的准确性,但很明显基于 IP 的地理定位有几个缺点:它不适用于本地 IP,如果用户在代理后面,它是会返回一个错误的位置,它并不总是很准确。我编辑了一项服务:userinfo.io,它为您提供来自 IP 的地理位置(和其他信息)。到目前为止,我的测试表明它非常准确,但它具有我提到的相同缺点。我依赖于几个 geoip 提供商,所以也许它会更准确一些,但我不能确定。

标签: javascript ajax html geolocation


【解决方案1】:

在我的网站上运行的代码。您似乎已禁用浏览器上的地理位置跟踪,直接将您发送到错误场景。请注意,这将要求用户明确授予您获取位置跟踪信息的权限。

【讨论】:

    【解决方案2】:

    我也尝试过测试地理位置,也从本地计算机打开 .html 文件 - Chrome 总是在这里给我错误回调:

    navigator.geolocation.getCurrentPosition(success, error);
    

    尽管启用了地理位置跟踪。 其他浏览器给出了成功回调。 然后我把页面放到主机上,它在所有浏览器中都返回了成功,包括谷歌浏览器。 当您的网站在本地运行时,您可能有类似的位置权限。

    【讨论】:

      猜你喜欢
      • 2012-06-14
      • 2015-03-12
      • 2015-10-17
      • 1970-01-01
      • 2022-12-24
      • 2019-03-11
      • 2013-02-07
      • 1970-01-01
      • 2012-04-17
      相关资源
      最近更新 更多