Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
11
rated 0 times [  17] [ 6]  / answers: 1 / hits: 46317  / 12 Years ago, wed, february 13, 2013, 12:00:00

I would like to hide() or show() a button that allows users to use their current location based on whether or not they are currently allowing location to be used in their browser setting.
the below code only checks if the browser supports geolocation and not whether or not the particular user is allowing it.



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


Is there a boolean value that I can detect for their browser setting letting me know if location is currently allowed?



thanks for any suggestions.


More From » jquery

 Answers
6

Have you read http://www.w3schools.com/html/html5_geolocation.asp



What you want to do is check the errors to see if they allowed it or denied the request.



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;
}
}

[#80240] Tuesday, February 12, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jimmieo

Total Points: 515
Total Questions: 102
Total Answers: 110

Location: Kazakhstan
Member since Mon, Sep 26, 2022
2 Years ago
;