I am building an app using PhoneGap. While using the Geolocation API of phonegap I realized that the APIs time out for two reasons and the same error is thrown: 1. If GPS is
To handle this case most gracefully, you can use cordova.plugins.diagnostic to check if GPS setting is enabled and (on Android 6+) check if app has run-time authorization, and (if it's not enabled), use cordova-plugin-request-location-accuracy to automatically switch on GPS via a native dialog without requiring users to manually switch it on via the settings page. However, since the latter relies on an up-to-date Google Play Services library on the device, it is good practice to fallback to the manual switching if the automatic switching fails.
First add the required plugins to your project:
cordova plugin add cordova.plugins.diagnostic --save
cordova plugin cordova-plugin-request-location-accuracy --save
Then you'd do it something like this:
function checkAvailability(){
cordova.plugins.diagnostic.isGpsLocationAvailable(function(available){
console.log("GPS location is " + (available ? "available" : "not available"));
if(!available){
checkAuthorization();
}else{
console.log("GPS location is ready to use");
}
}, function(error){
console.error("The following error occurred: "+error);
});
}
function checkAuthorization(){
cordova.plugins.diagnostic.isLocationAuthorized(function(authorized){
console.log("Location is " + (authorized ? "authorized" : "unauthorized"));
if(authorized){
checkDeviceSetting();
}else{
cordova.plugins.diagnostic.requestLocationAuthorization(function(status){
switch(status){
case cordova.plugins.diagnostic.permissionStatus.GRANTED:
console.log("Permission granted");
checkDeviceSetting();
break;
case cordova.plugins.diagnostic.permissionStatus.DENIED:
console.log("Permission denied");
// User denied permission
break;
case cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS:
console.log("Permission permanently denied");
// User denied permission permanently
break;
}
}, function(error){
console.error(error);
});
}
}, function(error){
console.error("The following error occurred: "+error);
});
}
function checkDeviceSetting(){
cordova.plugins.diagnostic.isGpsLocationEnabled(function(enabled){
console.log("GPS location setting is " + (enabled ? "enabled" : "disabled"));
if(!enabled){
cordova.plugins.locationAccuracy.request(function (success){
console.log("Successfully requested high accuracy location mode: "+success.message);
}, function onRequestFailure(error){
console.error("Accuracy request failed: error code="+error.code+"; error message="+error.message);
if(error.code !== cordova.plugins.locationAccuracy.ERROR_USER_DISAGREED){
if(confirm("Failed to automatically set Location Mode to 'High Accuracy'. Would you like to switch to the Location Settings page and do this manually?")){
cordova.plugins.diagnostic.switchToLocationSettings();
}
}
}, cordova.plugins.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY);
}
}, function(error){
console.error("The following error occurred: "+error);
});
}
checkAvailability(); // start the check