我想隐藏()或显示()一个按钮,允许用户根据他们当前是否允许在浏览器设置中使用位置来使用他们当前的位置.
以下代码仅检查浏览器是否支持地理定位,而不检查特定用户是否允许它.
以下代码仅检查浏览器是否支持地理定位,而不检查特定用户是否允许它.
if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else { x.innerHTML="Geolocation is not supported by this browser.";} }
是否有一个布尔值,我可以检测到他们的浏览器设置让我知道位置是否被允许?
感谢任何建议.
解决方法
你读过
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; } }