Request location coordinates after user has blocked access in javascript

后端 未结 2 434
时光说笑
时光说笑 2021-01-17 14:52

How can I prompt a user for their geo-location in javascript if they\'ve blocked my request in the past? (using navigator.geolocation.getCurrentPosition).

For exampl

2条回答
  •  我在风中等你
    2021-01-17 15:27

    As mentioned by @matthew-shwery, you can not change the permission.
    the best you could do is check for the permission and notify the user is the permission is denied

    navigator.permissions.query({
         name: 'geolocation'
     }).then(function(result) {
         if (result.state == 'granted') {
             report(result.state);
             geoBtn.style.display = 'none';
         } else if (result.state == 'prompt') {
             report(result.state);
             geoBtn.style.display = 'none';
    
             navigator.geolocation.getCurrentPosition(revealPosition, positionDenied, geoSettings);
         } else if (result.state == 'denied') {
             report(result.state);
             geoBtn.style.display = 'inline';
         }
         result.onchange = function() {
             report(result.state);
         }
     });
    

    Geolocation docs

提交回复
热议问题