I have an app working with Cordova, and I have installed this plugin from the official repository:
Plugin installation: cordova plugin add cordova-plugin-geolocation
Repository: https://github.com/apache/cordova-plugin-geolocation
Device ready event:
onDeviceReady: function() {
app.receivedEvent('deviceready');
console.log('deviceready');
loadPosition();
}
In the deviceready event I call to this function:
Load Position Function:
function loadPosition() {
// onSuccess Callback
// This method accepts a Position object, which contains the
// current GPS coordinates
//
var onSuccess = function (position) {
localStorage.setItem("latitude", position.coords.latitude);
localStorage.setItem("longitude", position.coords.longitude);
localStorage.setItem("position", true);
};
// onError Callback receives a PositionError object
//
function onError(error) {
localStorage.setItem("position", false);
alertErrorPosition();
}
function alertErrorPosition() {
navigator.notification.alert(
'Necesitamos utilizar tu ubicación para poder hacer funcionar la aplicación. Reinicia la aplicación o vuélve a instalarla . Gracias.', // message
null, // callback
'¡Error!', // title
'Ok' // buttonName
);
}
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
The problem:
On Android, I don't see the alert error if the App can't obtain the localization (for example, the GPS is not actived), but on iOS, if the user deny the access, I can see the error alert.
If the user has the GPS activated, I don't have any problem and the App obtain correctly the latitude and longitude.
I have tested it in the emulator and in a real device. I'm using Android Studio for testing.
Thanks!!