Javascript GeoLocation 不适用于 Chrome

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37273146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:08:29  来源:igfitidea点击:

Javascript GeoLocation is not working on Chrome

javascriptgoogle-chromegeolocation

提问by Konstantinos Natsios

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.

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

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");
  }
};

And i get the error 1

我得到了 error 1

User denied the request for Geolocation.

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

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

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

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

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

重新启动chrome,什么也没发生。我确定我的代码是 100% 合法的,所以有人知道如何处理吗?谢谢!

回答by Konstantinos Natsios

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

好的,这很容易... Chrome 自 2016 年 4 月 20 日起禁用了不安全网站的 Geolocation API(没有 https)...链接在这里

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

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

So dont worry..

所以别担心..

回答by Marina Dunst

The easiest way is to click on the area left to the address bar and change location settings there. It allows to set location options even for file:///and all other types

最简单的方法是单击地址栏左侧的区域并在那里更改位置设置。它允许甚至为file:///所有其他类型设置位置选项

enter image description here

在此处输入图片说明

回答by Atal Kishore

If you are using chrome, please have a look at the answer below:

如果您使用的是 chrome,请查看以下答案:

HTML 5 Geo Location Prompt in Chrome

Chrome 中的 HTML 5 地理位置提示