gpt4 book ai didi

javascript - 检查用户浏览器中的位置设置是否已关闭

转载 作者:可可西里 更新时间:2023-11-01 01:36:26 24 4
gpt4 key购买 nike

我想隐藏()或显示()一个按钮,允许用户根据他们当前是否允许在其浏览器设置中使用位置来使用他们的当前位置。下面的代码只检查浏览器是否支持地理定位,而不检查特定用户是否允许。

if (navigator.geolocation)  {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML="Geolocation is not supported by this browser.";}
}

我是否可以检测到他们的浏览器设置的 bool 值,让我知道当前是否允许定位?

感谢您的任何建议。

最佳答案

你读过http://www.w3schools.com/html/html5_geolocation.asp吗?

您要做的是检查错误以查看它们是允许还是拒绝请求。

function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition,showError);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}

function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
}

function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}

关于javascript - 检查用户浏览器中的位置设置是否已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14862019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com