check if location setting has been turned off in users browser

前端 未结 2 1346
悲哀的现实
悲哀的现实 2020-12-05 05:11

I would like to hide() or show() a button that allows users to use their current location based on whether or not they are currently allowing location to be used in their br

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 06:06

    The below code will allow you to check the permission status without invoking the navigator.geolocation permission request.

    Browsers Supported: Chrome(43+), Firefox(46+), Edge and Opera.

    Unsupported: Safari(mac, ios), Internet explorer, Android webview.

        navigator.permissions && navigator.permissions.query({name: 'geolocation'})
        .then(function(PermissionStatus) {
            if (PermissionStatus.state == 'granted') {
                  //allowed
            } else if (PermissionStatus.state == 'prompt') {
                  // prompt - not yet grated or denied
            } else {
                 //denied
            }
        })
    

    Here is the Reference Link.

    Compatibility on other browsers is unknown. I haven't tested it myself but please feel to test yourself and comment below.

提交回复
热议问题