Question - Proper flow to get location
Closed this issue · 2 comments
If I am not wrong, 'Permission Allow / Deny dialog box' will appear only when native geolocation gets called. So what will be the proper flow to get location. Requirement is that It should first show the dialog box for Allow / Deny of Location permission. Then, It should wait for user's action. On Deny - Some alert will appear. On Accept, it will check if location is on / off. If off - It will show the dialog box of location accuracy. If on - it should calculate the lat, lng.
What structure should I use to get the result as above?
Yes, the order of operation is:
- Check if location use is authorized
- If not request authorization
- Once, authorized check if location mode is ON and of sufficient accuracy
- If not, request the desired location accuracy to show the native location accuracy dialog box.
This plugin is intended as a compliment to cordova-diagnostic-plugin, and is best used in conjunction with it.
So you might do something like this:
var desiredAccuracy = cordova.plugins.diagnostic.locationMode.HIGH_ACCURACY;
function onError(error) {
var msg = "An error occurred: " + error;
console.error(msg);
alert(msg);
}
function checkAuthorization(){
cordova.plugins.diagnostic.isLocationAuthorized(function (authorized) {
if(!authorized){
cordova.plugins.diagnostic.requestLocationAuthorization(function (status) {
case cordova.plugins.diagnostic.permissionStatus.GRANTED:
console.log("Permission granted");
checkLocationAccuracy();
break;
case cordova.plugins.diagnostic.permissionStatus.DENIED:
console.log("Permission denied");
// Handle permission denied
break;
case cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS:
console.log("Permission permanently denied");
// Handle permission denied always
break;
}, onError);
}else{
checkLocationAccuracy();
}
}, onError);
}
function checkLocationMode(){
cordova.plugins.diagnostic.getLocationMode(function(locationMode){
if(locationMode !== desiredAccuracy){
requestLocationAccuracy();
}
}, onError);
}
function requestLocationAccuracy(){
cordova.plugins.locationAccuracy.canRequest(function(canRequest){
if(canRequest){
cordova.plugins.locationAccuracy.request(function(){
console.log("Successfully requested desired accuracy");
checkLocationMode(); //recheck mode
},
onError,
cordova.plugins.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY);
}
});
}
// Start the process
checkAuthorization();
Thanks! Seriously was not expecting such a detailed reply. Would suggest to put the above code in your example repo - https://github.com/dpa99c/cordova-plugin-request-location-accuracy-example