Test if html5 geolocation permission already has been granted

后端 未结 3 675
时光取名叫无心
时光取名叫无心 2021-01-02 23:22

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 unles

3条回答
  •  没有蜡笔的小新
    2021-01-02 23:36

    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 */
            })
        }
    })
    

提交回复
热议问题