【问题标题】:navigator.geolocation.getCurrentPosition/watchPosition is not working in android 6.0navigator.geolocation.getCurrentPosition/watchPosition 在 android 6.0 中不起作用
【发布时间】:2017-03-08 11:09:24
【问题描述】:

这是我的 javascript 代码:

function getLocation() { 
    //navigator.geolocation.getCurrentPosition(getCoor, errorCoor, {maximumAge:60000, timeout:30000, enableHighAccuracy:true});
    var mobile =jQuery.browser.mobile;
    var deviceAgent = navigator.userAgent.toLowerCase();
    var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
    if(mobile){
        watchLocation(function(coords) {
        var latlon = coords.latitude + ',' + coords.longitude;
         //some stuff
      }, function() {
        alert("error");
      });
    } else {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        } else {
            alert("error");
        }
    }
}

function watchLocation(successCallback, errorCallback) { 
    successCallback = successCallback || function(){};
    errorCallback = errorCallback || function(){}; 
    // Try HTML5-spec geolocation.
    var geolocation = navigator.geolocation; 
    if (geolocation) {
        // We have a real geolocation service. 
        try {
          function handleSuccess(position) {
            alert("position:"+position.coords); 
            successCallback(position.coords);
          }  
          geolocation.watchPosition(handleSuccess, errorCallback, {
            enableHighAccuracy: true,
            maximumAge: 5000 // 5 sec.
          }); 
        } catch (err) { 
            errorCallback();
        }
    } else {  
        errorCallback();
    }
}

getCurrentPositionwatchPosition 我都试过了。

当控制到达geolocation.watchPosition 行时,它到达errorCalback() 方法。

我正在使用Android 6Google chrome browseropera miniMotorola G 2nd Gen 中进行测试。

更新 1:当我将警报放入错误回调函数时,我得到了error:1; message:Only Secure origins are allowed(see:link)。

    navigator.geolocation.getCurrentPosition(showPosition, function(e)
    {  alert(e); //alerts error:1; message:Only Secure origins are allowed(see:  )
       console.error(e);
    })

更新 2:g4s8 的帮助下,我能够发现错误是由于 URL 不安全造成的。即仅使用http 而不是https 访问。但是我也通过单击高级按钮在浏览器中绕过了它。但它会提示我不想要的Do you want to allow location。有什么方法可以在不提示的情况下访问位置?

【问题讨论】:

  • 您是否尝试将第二个参数(错误回调)添加到 getCurrentPosition 方法? navigator.geolocation.getCurrentPosition(showPosition, function(e) {console.error(e);})
  • 是的...你可以看到被注释的第二行...errorCoor是那个回调函数...
  • 您能否将错误打印到控制台并在此问题中添加错误消息?它可能包含有用的信息。例如PositionError {code: 1, message: "User denied Geolocation"}
  • 您可以从谷歌浏览器连接到安卓设备。看到这个链接developers.google.com/web/tools/chrome-devtools/…
  • 在这种情况下,您可以在警报中显示错误代码和消息:function (err) {alert("error: " + err.code + "; message: " + err.message);}

标签: javascript android html geolocation


【解决方案1】:

您的页面应通过 https 提供以访问地理位置 API。

Geolocation API Removed from Unsecured Origins

从 Chrome 50 开始,Chrome 不再支持使用 HTML5 Geolocation API 从非安全连接传递的页面获取用户位置

...

这是一个重要的问题,因为它将直接影响任何需要使用地理定位 API 并且不通过 h​​ttps 提供服务的站点

要解决此问题,请通过 https 或 localhost 提供您的页面。


谢谢...有什么办法可以绕过吗??

您可以尝试使用一些地理定位服务,例如 geoip2, Geolocation request


如何使用它们?你能举个例子吗?从这两个我可以在不知道他们的情况下访问用户位置吗?

GeoIP2 通过 IP 地址检测您的位置。可以通过js lib获取国家(geoip2.country())和城市(geoip2.city):

<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>

在这里https://dev.maxmind.com/geoip/geoip2/javascript/ 你可以找到完整的文档。

谷歌地图地理位置是谷歌服务,所以你需要先get api key。然后你可以发送带有json参数的POST请求到https://www.googleapis.com/geolocation/v1/geolocate?key=API_KEY并得到响应:

{
  "location": {
    "lat": 51.0,
    "lng": -0.1
  },
  "accuracy": 1200.4
}

其中位置是用户估计的经纬度,以度为单位, 准确度是估计位置的准确度,以米为单位。

完整的 json 参数定义可以在此处的“请求正文”部分找到 https://developers.google.com/maps/documentation/geolocation/intro#overview

您还可以找到有用的答案:getCurrentPosition() and watchPosition() are deprecated on insecure origins


使用 IP 它只提供国家和城市..??

是的,只有这个。

它会像 getCurrent Position 一样提供物理位置吗??

不,您无法获取物理位置,因为它只能通过地理定位 API 访问,这在不安全的上下文中受到限制。

您还有更多选择。您只能在 https 服务器上托管一个页面(访问地理定位 API),并从该页面重定向到您的 http 站点,并在获取参数中使用用户位置。

/* https page */
navigator.geolocation.getCurrentPosition(function (result) {
    window.location.href = "http://your.site.com/http-page?lat=" + result.latitude + "&long=" + result.longitude;
});

【讨论】:

  • 谢谢...有什么办法可以绕过吗??
  • 如何使用它们?你能举个例子吗?从这两个我可以在不知道他们的情况下访问用户位置吗?
  • g4s8 使用 IP 它只提供国家和城市..??它会像 getCurrent Position 一样提供物理位置吗??
猜你喜欢
  • 2018-11-14
  • 2013-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多