Is there a way of detecting whether a user has already given permission to use navigator.geolocation?
Asked Answered
E

3

18

Apart from setting a cookie the first time round, is there a way of detecting whether a user has already given permission for navigator.geolocation to return the lat/long of the browser?

If there is, what is it and is it the same across all browsers or different across all browsers?

This subject has been partially answered elsewhere

According to GeoLocation API – Chrome / Safari – Permission management and Visual Differences, Chrome asks for a revokable one-time permission. I haven't finished reading the article, but it would seem that storage of permissions is not a purely-Chrome thing to do.

Embattled answered 14/5, 2013 at 7:21 Comment(0)
A
27

What about using localStorage which should be supported by all html5 browsers (that support geoLocation)

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
} 
//Get latitude and longitude;
function successFunction(position) {
    var lat = position.coords.latitude;
    var long = position.coords.longitude;

    localStorage['authorizedGeoLocation'] = 1;
}

function errorFunction(){
    localStorage['authorizedGeoLocation'] = 0;
}

function checkauthorizedGeoLocation(){ // you can use this function to know if geoLocation was previously allowed
    if(typeof localStorage['authorizedGeoLocation'] == "undefined" || localStorage['authorizedGeoLocation'] == "0" ) 
        return false;
    else 
        return true;
}

And then you check using the below function :

alert(checkauthorizedGeoLocation());

This is the jsfiddle if you need to check

Anaclinal answered 21/5, 2013 at 21:25 Comment(10)
Now we're getting somewhere!!Embattled
I hope it helps :) let me know if that is what you need.Anaclinal
Mehdi, I'm giving you the bounty. It's been pointed out to me that the HTML version that gives me geolocation is the one that also gives me localStorage.Embattled
@Mehndi: this POC will give error condition if user clear location setting from browser. In this case user still gets true from local storage even though location settings are cleared.Derna
@ShobhaSingh That is expected, isn't it ?Anaclinal
This doesn't distinguish if the permission was given permanently or not. If the user gives permission for just this visit then upon their next visit you will have the wrong value.Redmond
@TimCharters this was posted more than 2 years ago, not sure if giving permission for just one visit feature existed back then, but please feel free to suggest other solutionsAnaclinal
@MehdiKaramosly Indeed, though people are still looking at it. And wrong way round - iStuff seems to not be able to do permanent. It doesn't look like there is a solution unfortunately.Redmond
Dang, nice bounty there. Thanks for the function, this is exactly what I needed.Derris
@CaptainHypertext Thanks I am glad you like it and it resolve your problem.Anaclinal
B
4

According to the spec, no - there's just the three methods on navigator.geolocation. However saving to a cookie or local storage is probably perfectly suitable - the permission is also stored in the user agent, so it should work correctly as the user moves between browsers.

Baneful answered 18/5, 2013 at 14:20 Comment(2)
However, the "Prompting for permission" section in developer.mozilla.org/en-US/docs/WebAPI/… would appear to imply that at least one browser has a mechanism to store permission.Embattled
That appears to only apply to Firefox addons, not general web content.Baneful
L
2

Please see this answer on how to do it using the Permissions API: Check if Geolocation was allowed and get Lat Lon

Works without prompting the user if permission was granted previously.

Lema answered 14/9, 2017 at 4:20 Comment(1)
Note however that Permission API is not supported by all major browsers, namely Safari and iOS Safari: caniuse.com/#feat=permissions-apiAramaic

© 2022 - 2024 — McMap. All rights reserved.