Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if html5 geolocation permission already has been granted

Does anyone know if there is a way to test if prior html5 geolocation permission has been granted?

I try to make a script that does not request the geolocation unless the permission for the page has already been given. Does not nessicerily have to be html5; other frameworks are also ok.

like image 429
RickyA Avatar asked May 18 '12 14:05

RickyA


People also ask

How do I get users to grant me geolocation permission?

Always request geolocation permission after a user action, not on page load. Clearly indicate that the action will request geolocation permission. Assume users won't give you their locations. Use a fallback if users don't grant geolocation permission.

Why can't lighthouse identify any geolocation permission requests?

If geolocation permission was granted to a page before the audit, Lighthouse can't identify any geolocation permission requests. So, make sure to reset permissions before running the audit.

What is HTML5 Geolocation API?

HTML5 Geolocation API The Geolocation API gives websites access to a high-level interface that allows them to query physical location information such as latitude and longitude, thereby breaking the user's anonymity.

How to use geolocation testing in Google Chrome?

To change the location in Google Chrome, press Ctrl + Shift + P ( Cmd + Shift +P ). Type “ Sensors ” to open up the sensors list from which you have to select “Geolocation”. In Geolocation, a list will open up to choose a location from. Geolocation testing today stands on similar priorities as other types of testing.


2 Answers

The first part actually answers your question, at least on Chrome 43+ and Firefox 46+ (see https://developer.mozilla.org/en-US/docs/Web/API/Permissions_API):

navigator.permissions &&
navigator.permissions.query({name: 'geolocation'}).then(function(PermissionStatus) {
    if('granted' === PermissionStatus.state) {
        navigator.geolocation.getCurrentPosition(function(geoposition) {
            console.log(geoposition) /* You can use this position without prompting the user if the permission had already been granted */
        })
    }
})
like image 66
Sébastien Fauvel Avatar answered Oct 19 '22 22:10

Sébastien Fauvel


Best you can do is to keep track of this yourself...

// In chrome you can now do this    
navigator.permissions.query({name: 'geolocation'}).then(function(PermissionStatus){
    console.log(PermissionStatus.state) // prompt, granted, denied
    // even listen for changes
    PermissionStatus.onchange = function(){
        console.log(this.state)
    }
})

fallback method:

// initialization
if( sessionStorage.getItem("geo_access") === null ){
    // just assume it is prompt
    sessionStorage.setItem("geo_access", "prompt");
}

function ask(){
    navigator.geolocation.getCurrentPosition(function(){
        sessionStorage.setItem("geo_access", "granted");

    }, function(err){
        if(err.code == 1){ // PERMISSION_DENIED
            sessionStorage.setItem("geo_access", "denied");
        }
        sessionStorage.setItem("geo_access", "prompt");
    });
};

// Then somewhere
ask();
like image 27
Endless Avatar answered Oct 19 '22 23:10

Endless