Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request permission geolocation plugin cordova

I want know if there is a way to ask again the permission location dialog to the user, after deny the first time using this plugin of cordova:

https://github.com/apache/cordova-plugin-geolocation

thanks

like image 401
Piero Avatar asked Mar 10 '23 19:03

Piero


1 Answers

You could do this using cordova-diagnostic-plugin, for example:

var deniedCount = 0;

function onError(error){
    console.error("The following error occurred: "+error);
}

function evaluateAuthorizationStatus(status){
    switch(status){
       case cordova.plugins.diagnostic.permissionStatus.NOT_REQUESTED:
           console.log("Permission not requested");
           requestAuthorization();
           break;
       case cordova.plugins.diagnostic.permissionStatus.DENIED:
           console.log("Permission denied");
           if(deniedCount < 3){
              deniedCount++;
              requestAuthorization();
           }else{
              // Are we sure we want to hassle the user more than 3 times?
           }
           break;
       case cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS:
           console.log("Permission permanently denied");
           navigator.notification.confirm(
                "This app has been denied access to your location and it really needs it function properly. Would you like to switch to the app settings page to allow access?", 
                function (i) {
                    if (i === 1) {
                        cordova.plugins.diagnostic.switchToSettings();
                    }
                }, "Location access denied", ['Yes', 'No']);
           break;
       case cordova.plugins.diagnostic.permissionStatus.GRANTED:
           console.log("Permission granted always");
           // Yay! use location
           break;        
   }
}

function requestAuthorization(){
    cordova.plugins.diagnostic.requestLocationAuthorization(evaluateAuthorizationStatus, onError);
}

function checkAuthorization(){
    cordova.plugins.diagnostic.getLocationAuthorizationStatus(evaluateAuthorizationStatus, onError);
}

checkAuthorization();
like image 185
DaveAlden Avatar answered Mar 19 '23 06:03

DaveAlden