Check if Geolocation was allowed and get Lat Lon

I'm building a small script that clients will install on their page. Geolocation is not necessary for it, however if it exists it would be nice to have. Is there a way for me to check if the clients page has requested geolocation information and if the user selected allow get the lat & lon without creating another prompt?

Answers:

Answer

It isn't possible with the geolocation API but it is possible with the new permission API

This code is useful if you want to try to receive the coordinates but don't want to bother the user with a prompt dialog cuz the coordinates is not that important and may as well fallback to geoIP

function getCoords() {
  return new Promise((resolve, reject) =>
    navigator.permissions ?

      // Permission API is implemented
      navigator.permissions.query({
        name: 'geolocation'
      }).then(permission =>
        // is geolocation granted?
        permission.state === "granted"
          ? navigator.geolocation.getCurrentPosition(pos => resolve(pos.coords)) 
          : resolve(null)
      ) :

    // Permission API was not implemented
    reject(new Error("Permission API is not supported"))
  )
}

getCoords().then(coords => console.log(coords))

This will resolve to null if permission hasn't been granted, cast a error if some javascript code wasn't supported by the browser and resolve to the coordinates if it has been granted

Answer

You can detect if the feature exists by examining the window object.

if('geolocation' in window){
     navigator.geolocation.getCurrentPosition(...);
}

This will cause a single user prompt to come up asking the user if they wish to share their location with your page.

If geolocation is not available in the browser, this will not run at all.


EDIT If you've already prompted the user on this page load for allowing geolocation access, it will NOT prompt you again. If the user navigates away and back to this page, it will re-prompt them.

You may make subsequent calls to the API without prompting during that page session.

Reference: MDN Geolocation -- Watching the current position

Answer

According to the API it is not possible to find out of the user has already permitted the current site to retrieve the user's location without causing a prompt if he hasn't. See Endless answer

I'd suggest you to allow users to pass an argument when embedding your script telling it if it should use geolocation features or not.

Answer

I also encountered the same problem. And, after I search and experiment I finally found the answer.

You can add this JS code :

navigator.permissions.query({name:'geolocation'}).then(function(result) {
  // Will return ['granted', 'prompt', 'denied']
  console.log(result.state);
});

Then you can use your custom code as needed.

source : https://developer.mozilla.org/en-US/docs/Web/API/Navigator/permissions

Answer

Whether or not you're prompted seems to depend on the browser and what 'privacy' settings it's got. Safari 6 has 3 settings: prompt once per site, prompt once per site per day, and deny. Chrome 23 has 3 settings: allow all, ask, and deny all. Sure would get good if there was a way to check if it's already allowed without bothering the user.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.