navigator.geolocation.getCurrentPosition / watchPosition在android 6.0中不起作用

时间:2016-10-26 07:32:37

标签: javascript android html5 geolocation

这是我的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

当控件进入errorCalback()行时,它会触及geolocation.watchPosition方法。

我正在使用Motorola G 2nd GenAndroid 6以及Google chrome browseropera mini进行测试。

更新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的帮助下,我能够发现错误是由于网址不安全造成的。即仅使用http代替https进行访问。但是我也通过点击高级按钮在浏览器中绕过了它。但它会提示 Do you want to allow location ,我不想要..有没有办法在没有提示的情况下访问位置?

1 个答案:

答案 0 :(得分:5)

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

请参阅Geolocation API Removed from Unsecured Origins

  

从Chrome 50开始,Chrome不再支持使用来自非安全连接提供的网页的HTML5地理位置API获取用户的位置

     

...

     

这是一个重要问题,因为它会直接影响任何需要使用地理位置API并且不通过https提供的网站

要解决此问题,请通过https或localhost

为您的网页提供服务
  

谢谢...有没有办法绕过它?

您可以尝试使用某些地理位置服务,例如 geoip2Geolocation 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/您可以找到完整的文档。

Google地图地理位置是Google服务,因此您需要先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
}

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

你可以在&#34; Request body&#34;中找到完整的json参数defenition。此处https://developers.google.com/maps/documentation/geolocation/intro#overview

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

  

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

是的,只有这一点。

  

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

不,你不能获得物理位置,因为它只能通过gelocation API访问,这在不安全的上下文中受到限制。

此外,您还有一个选项。您只能在https服务器上托管一个页面(访问地理位置API),并从此页面重定向到具有get参数中用户位置的http站点。

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