16

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?

tkone
  • 19,271
  • 5
  • 50
  • 77
Stefan
  • 2,643
  • 1
  • 18
  • 34

5 Answers5

27

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

Endless
  • 24,091
  • 10
  • 82
  • 106
  • 3
    This is a handy approach but - since it's a rather new API - it is important to mention the possible compatibility issues. For example: http://caniuse.com/#feat=permissions-api – Mario Apr 08 '16 at 07:17
6

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

tkone
  • 19,271
  • 5
  • 50
  • 77
  • I don't think he cares if the browser supports it or not - he wants to know if, in case the browser supports it, he can call `getCurrentPosition()` *without* a prompt because the site already caused such a prompt before. – ThiefMaster Apr 09 '12 at 18:14
  • @ThiefMaster. That's not a downvote unless you asked the question. The intention of the question asker is not clear. In that case, however, please see my update. – tkone Apr 09 '12 at 18:23
  • 1
    I think `geolocation` is in navigator object, NOT in window object. – Ĭsααc tիε βöss Mar 28 '17 at 13:42
4

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.

Community
  • 1
  • 1
ThiefMaster
  • 285,213
  • 77
  • 557
  • 610
4

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

Fendi Septiawan
  • 363
  • 3
  • 5
2

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.

OsamaBinLogin
  • 511
  • 4
  • 10