Javascript GeoLocation不适用于Chrome [英] Javascript GeoLocation is not working on Chrome

查看:904
本文介绍了Javascript GeoLocation不适用于Chrome的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试取用户的地理位置,然后进行查询。
在Mozilla Firefox中,它在Safari中也能正常工作......但在Chrome中它根本不起作用。

I'm trying to take the geolocation of the User and then do a query. In Mozilla Firefox it works fine also in Safari.... but in Chrome it doesnt work at all.

window.onload = function(){
  if(navigator.geolocation){

    navigator.geolocation.getCurrentPosition(function(position){

        var latitude = position.coords.latitude,
            longitude = position.coords.longitude;
      console.log(latitude + " " + longitude);

    },handleError);

    function handleError(error){
      //Handle Errors
     switch(error.code) {
        case error.PERMISSION_DENIED:
            console.log("User denied the request for Geolocation.");
            break;
        case error.POSITION_UNAVAILABLE:
            console.log("Location information is unavailable.");
            break;
        case error.TIMEOUT:
           console.log("The request to get user location timed out.");
            break;
        case error.UNKNOWN_ERROR:
           console.log("An unknown error occurred.");
            break;
    }
    }
  }else{
    // container.innerHTML = "Geolocation is not Supported for this browser/OS.";
    alert("Geolocation is not Supported for this browser/OS");
  }
};

我得到错误1

User denied the request for Geolocation.

但我没有拒绝任何请求,实际上弹出窗口根本没有出现。

But i haven't denied any request, actually the popup window doesnt come up at all.

我已转至Chrome设置>显示高级>隐私>内容设置>允许所有位置。

I've went to Chrome Settings > Show advanced > Privacy > Content Settings > Location allow for all.

重新启动铬和什么都没有发生。我确定我的代码是100%合法的,所以有人知道如何处理它?
Thanks!

Restarted chrome and nothing happened. I'm sure my code is 100% legit so does anyone know how to deal with it? Thanks!

推荐答案

好的,这很简单...自2016年4月20日起,Chrome停用了Geolocation API对于不安全的网站(没有https)... link here

Ok this was quite easy... Chrome since 20 of April 2016 has disabled the Geolocation API for insecure websites (without https)... link here

https://developers.google.com/web/updates/2016/04/geolocation-on-secure-contexts-only

所以不要担心..

这篇关于Javascript GeoLocation不适用于Chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆