最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Test if html5 geolocation permission already has been granted - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question asked May 18, 2012 at 14:19 RickyARickyA 16k5 gold badges76 silver badges97 bronze badges 3
  • 1 What about storing the result of the original attempt to get the location? – alex Commented May 18, 2012 at 14:24
  • 1 I never want to be the one that asks for the permission in the first place. Its a 3th party script... – RickyA Commented May 18, 2012 at 14:26
  • stackoverflow./questions/16537282/… – Captain Hypertext Commented Nov 1, 2016 at 14:43
Add a ment  | 

3 Answers 3

Reset to default 12

The first part actually answers your question, at least on Chrome 43+ and Firefox 46+ (see https://developer.mozilla/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 */
        })
    }
})

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();

According to the Geolocation API Specification there isn't any function for this. Even the option to save it in the browser preferences isn't pushed to your code.

If it's realy important and you realy have to beeing aware of this, you have to write a browser addon for every mon browser and ask the user to install it. But i wouldn't doubt of it.

发布评论

评论列表(0)

  1. 暂无评论