Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if location setting has been turned off in users browser

Tags:

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.

like image 821
Tom Avatar asked Feb 13 '13 20:02

Tom


People also ask

How do you check location is enabled or not in Javascript?

If supported, run the getCurrentPosition() method. If not, display a message to the user. The showError() functiona displays the error message.

How do browsers detect location?

Your browser uses different types and sources of information to identify your location. These include your IP address, geolocation via HTML5 in your browser, and your PC's language and time settings.

How do I find the location of a user?

In order to get the last location of the user, make use of the Java public class FusedLocationProviderClient. It is actually a location service that combines GPS location and network location to achieve a balance between battery consumption and accuracy.

How accurate is location from browser?

IP-based geolocation services provide 55 percent to 80 percent accuracy for a user's region or state. And they provide 50 percent to 75 percent accuracy for a user's city. In practice, the actual accuracy may vary from provider to provider and depending on the location of the device.


2 Answers

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;   } } 
like image 60
Bot Avatar answered Nov 09 '22 10:11

Bot


The below code will allow you to check the permission status without invoking the navigator.geolocation permission request.

Browsers Supported: Chrome(43+), Firefox(46+), Edge and Opera.

Unsupported: Safari(mac, ios), Internet explorer, Android webview.

    navigator.permissions && navigator.permissions.query({name: 'geolocation'})     .then(function(PermissionStatus) {         if (PermissionStatus.state == 'granted') {               //allowed         } else if (PermissionStatus.state == 'prompt') {               // prompt - not yet grated or denied         } else {              //denied         }     }) 

Here is the Reference Link.

Compatibility on other browsers is unknown. I haven't tested it myself but please feel to test yourself and comment below.

like image 29
Noushad Avatar answered Nov 09 '22 12:11

Noushad