Javascript GeoLocation 不适用于 Chrome

新手上路,请多包涵

我正在尝试获取用户的地理位置,然后进行查询。在 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% 合法的,所以有人知道如何处理它吗?谢谢!

原文由 Konstantinos Natsios 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 398
2 个回答

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

在此处输入图像描述

原文由 Marina Dunst 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题