Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 704
bugmagnet Avatar asked May 14 '13 07:05

bugmagnet


2 Answers

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

like image 178
Mehdi Karamosly Avatar answered Oct 13 '22 23:10

Mehdi Karamosly


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.

like image 20
AshleysBrain Avatar answered Oct 13 '22 22:10

AshleysBrain