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;
  }
}
Answer from Bot on Stack Overflow
Top answer
1 of 2
45

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;
  }
}
2 of 2
32

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.

๐ŸŒ
findnerd
findnerd.com โ€บ list โ€บ view โ€บ how-to-identify-location-is-enable-or-not-on-browser-with-jQuery โ€บ 10369
how to identify location is enable or not on browser with jQuery?
November 18, 2015 - <script> /* Get current location*/ function getCurrentLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(success,showError); } else { alert("Geolocation is not supported on your browser!"); } function success(position) { alert("Latitude: " + position.coords.latitude +" Longitude: " + position.coords.longitude); } function showError(error) { switch(error.code) { case error.PERMISSION_DENIED: alert("Comments are only permitted when location sharing is enabled- you have denied the request for Geolocation."); break; case error.POSITION_UNAVAILABLE: alert("Comments a
Discussions

javascript - Function to determine if geo-location is enabled - Stack Overflow
I have a function that determines if the users broswer is geo-location enabled and if the user has geo-location enabled it is to display a Google map, if it isn't it then displays a different map. More on stackoverflow.com
๐ŸŒ stackoverflow.com
April 10, 2013
javascript - Detect if user has enabled location services on domain - Stack Overflow
Is there a way to know if a user has already enabled location services for a domain in a web page script. This is for purpose of NOT showing a button "detect location" if the user has already pressed it once in the past. ... You would need to set a cookie with a value and basically check to see ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - Is there a way to know if geolocation is enabled for the current domain? - Stack Overflow
The website I'm working on uses HTML5 geolocation api. It works fine, but I need to know if the feature has been authorized. navigator.geolocation only tells if the feature is available. What I wan... More on stackoverflow.com
๐ŸŒ stackoverflow.com
August 20, 2013
Is it possiblel to detect when a user has disabled location services in their OS?
You should be able to use the error callback for this. More on reddit.com
๐ŸŒ r/learnjavascript
4
1
December 11, 2023
People also ask

How can I get a user's location with JavaScript?
To get a user's location using JavaScript, you can use the built-in 'navigator.geolocation' object, which provides access to the device's location. Another way is to call IP Geolocation API which returns the user's location with accuracy up to the city level.
๐ŸŒ
geoapify.com
geoapify.com โ€บ how-to-get-user-location-with-javascript
How to Get User Location with JavaScript | Geoapify
What is the difference between navigator.geolocation and IP geolocation?
The main difference between 'navigator.geolocation' and IP geolocation is the way they determine the user's location. The 'navigator.geolocation' relies on the GPS receiver of the device, which provides accurate real-time location data. On the other hand, IP geolocation uses the user's IP address to estimate their location, which is usually accurate only at the city or region level. Additionally, 'navigator.geolocation' requires the user's explicit consent to share their location, while IP geolocation does not.
๐ŸŒ
geoapify.com
geoapify.com โ€บ how-to-get-user-location-with-javascript
How to Get User Location with JavaScript | Geoapify
Can I get a user's precise location without their permission?
No, using a user's location without their permission is not ethical or legal. Location data is considered sensitive information, and users must grant explicit consent to share their location. In addition, unauthorized access to a user's location can violate their privacy and potentially put them in danger.
๐ŸŒ
geoapify.com
geoapify.com โ€บ how-to-get-user-location-with-javascript
How to Get User Location with JavaScript | Geoapify
๐ŸŒ
DEV Community
dev.to โ€บ sprite421 โ€บ how-to-check-a-user-s-location-with-javascript-142b
How to Check a User's Location with JavaScript - DEV Community
January 24, 2021 - We want to be able to click this button and get location data in the console. To achieve this, we'll write three JavaScript functions. The first is the function we'll stick to our button via event listener. We can access the Geolocation API at navigator.geolocation. Inside our first function, we'll check whether the Geolocation API is supported by testing navigator.geolocation, and if it is supported, we can get our location data using navigator.geolocation.getCurrentPosition().
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 15933874 โ€บ function-to-determine-if-geo-location-is-enabled
javascript - Function to determine if geo-location is enabled - Stack Overflow
April 10, 2013 - function init_maps() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(geoInfo, noGeoInfo, { timeout: 20000 }); } else { noGeoInfo(); } function geoInfo(position) { navigator.geolocation.getCurrentPosition(function (position) { var latitude = position.coords.latitude; var longitude = position.coords.longitude; var coords = new google.maps.LatLng(latitude, longitude); var directionsService = new google.maps.DirectionsService(); var directionsDisplay = new google.maps.DirectionsRenderer(); var mapOptions = { zoom: 15, center: coords, mapTypeControl: true, navigationControlO
๐ŸŒ
Geoapify
geoapify.com โ€บ how-to-get-user-location-with-javascript
How to Get User Location with JavaScript | Geoapify
April 1, 2026 - It is supported by most modern web browsers and can be accessed using JavaScript. It's important to note that user location is sensitive information, so the user must first grant permission for a web application to access it. Most modern web browsers will prompt users to grant or deny location-sharing permissions when a page tries to access navigator.geolocation. Here's an example code snippet that demonstrates how to get a user position with the navigator.geolocation interface: // Check if geolocation is supported by the browser if ("geolocation" in navigator) { // Prompt user for permission to access their location navigator.geolocation.getCurrentPosition( // Success callback function (position) => { // Get the user's latitude and longitude coordinates const lat = position.coords.latitude; const lng = position.coords.longitude; // Do something with the location data, e.g.
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ is it possiblel to detect when a user has disabled location services in their os?
r/learnjavascript on Reddit: Is it possiblel to detect when a user has disabled location services in their OS?
December 11, 2023 -

I just spent a good hour banging my head against a wall trying to work out why the callback in my navigator.geolocation.getCurrentPosition() method wasn't running. I was using Firefox and eventually thought to check in Chrome which immediatley notified me that I had location services disabled in my MacOs settings. (Doh!)

I had checked that location services were available using if ('geolocation' in navigator) but obviously this must not take into account whether a user has disabled location services directly in the OS of their device.

So my question is, how would I check this before offering this functinality on my page?

The user experiece in Chrome wouldn't be too bad, since it informs you straight away what the issue is, but the experience in Firefox would be awful for an end user because there was absolutely no feedback at all about what went wrong.

๐ŸŒ
Esri Community
community.esri.com โ€บ t5 โ€บ arcgis-web-appbuilder-questions โ€บ detecting-if-location-services-on-mobile-devices โ€บ td-p โ€บ 722884
detecting if location services on mobile devices are enabled
December 12, 2021 - Does anyone know a good way to check to see if location services are enabled/disabled on a mobile device? I have tried using the following with no luck: var isMobile = window.matchMedia("only screen and (max-width: 760px)"); if (isMobile.matches) { if (!window.navigator.geolocation) { this._displayWidgetError(this.nls.noGPSMobileContent); } } โ€โ€โ€โ€โ€โ€โ€โ€โ€โ€โ€โ€โ€
๐ŸŒ
Tutorialzine
tutorialzine.com โ€บ 2016 โ€บ 06 โ€บ quick-tip-detecting-your-location-with-javascript
Quick Tip: Detecting Your Location With JavaScript - Tutorialzine
June 22, 2016 - The Geolocation API has almost ... object reside all of the methods for the API: Geolocation.getCurrentPosition() - Determines the device's current location....
๐ŸŒ
W3Schools
w3schools.com โ€บ html โ€บ html5_geolocation.asp
HTML Geolocation API
Geolocation is also very useful for location-specific information, like: ... The getCurrentPosition() method returns an object on success. The latitude, longitude and accuracy properties are always returned.
๐ŸŒ
Reddit
reddit.com โ€บ r/shortcuts โ€บ check if location services is disabled
r/shortcuts on Reddit: check IF location services is disabled
June 7, 2022 -

Quick question is there anyway to check IF location services is disabled or if location is not accessible or something similar?

Basically I want to open the location setting page whenever I open google maps (which I have done) but the problem is that it opens the settings even if location is already on and I want it to only open that page if location setting is disabled. Worst case I guess I can just use the โ€œask before runningโ€ feature as a workaround

๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ javascript-geolocation-api-get-users-location-habibur-rahman
JavaScript Geolocation API - Get the User's Location
March 19, 2023 - You can request permission from the user by calling the getCurrentPosition() method and passing in an options object with the enableHighAccuracy property set to true. This will prompt the user to allow or deny location access.
๐ŸŒ
Techaltum
tutorial.techaltum.com โ€บ html5-geolocation.html
HTML5 Geolocation API Tutorial - Get Current Location in JavaScript
Geolocation is supported on https protocol & HTML5-based browsers only. For development purposes, Chrome allows geolocation in file protocol and localhost (i.e., 127.0.0.1). IE 8 and below do not support the HTML5 Geolocation API. For Production purpose, use https protocol. ... <script> if(navigator.geolocation) { alert("geolocation supported") } else{ alert("geolocation not supported") } </script>