我正在尝试获取用户的地理位置,然后进行查询。在 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 许可协议
好吧,这很简单……自 2016 年 4 月 20 日起,Chrome 已禁用不安全网站的地理定位 API(无 https)……链接在这里
https://developers.google.com/web/updates/2016/04/geolocation-on-secure-contexts-only
所以别担心..