Check if Geolocation was allowed and get Lat Lon
Asked Answered
T

5

20

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?

Truss answered 9/4, 2012 at 18:7 Comment(0)
S
33

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

Scarabaeid answered 25/2, 2016 at 13:22 Comment(1)
This is a handy approach but - since it's a rather new API - it is important to mention the possible compatibility issues. For example: caniuse.com/#feat=permissions-apiArmand
P
7

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

if ('geolocation' in navigator) {
     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

Phosphorism answered 9/4, 2012 at 18:10 Comment(3)
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.Fanniefannin
@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.Phosphorism
I think geolocation is in navigator object, NOT in window object.Laliberte
A
7

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

Aorta answered 3/5, 2018 at 9:43 Comment(1)
perfect! thank you.Caroylncarp
F
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.

Fanniefannin answered 9/4, 2012 at 18:16 Comment(0)
D
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.

Disconnection answered 3/12, 2012 at 23:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.