Javascript GeoLocation无法在Chrome上运行

时间:2016-05-17 10:10:09

标签: javascript google-chrome geolocation

我正在尝试接受用户的地理位置,然后进行查询。 在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");
  }
};

我得到了error 1

User denied the request for Geolocation.

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

我去了Chrome设置>显示高级>隐私>内容设置>位置允许所有。

重新启动chrome并没有发生任何事情。我确定我的代码是100%合法的,所以有谁知道如何处理它? 谢谢!

3 个答案:

答案 0 :(得分:3)

好的,这很简单......自2016年4月20日起,Chrome已禁用不安全网站的Geolocation API(不使用https)...链接此处

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

所以别担心..

答案 1 :(得分:2)

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

设置位置选项

enter image description here

答案 2 :(得分:1)

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

HTML 5 Geo Location Prompt in Chrome