Got a bit of an issue trying to determine if someone has their GPS turned off in Ionic/Cordova.
Here is what I am trying to do at the moment, this works in the browser fine but not on android.
navigator.geolocation.getCurrentPosition(function(pos) {}, function(err) {
alert('We were unable to determine your location, please turn on your GPS/Location services');
});
Another thing is does anyone know if its possible to give a prompt to turn on GPS with Ionic/cordova?
Anyone got any ideas?
Please add this plugin,
cordova plugin add cordova.plugins.diagnostic
1.Determine if someone has their GPS turned off in Ionic/Cordova
Contoller
if (window.cordova) {
cordova.plugins.diagnostic.isLocationEnabled(function(enabled) {
alert("Location is " + (enabled ? "enabled" : "disabled"));
}, function(error) {
alert("The following error occurred: " + error);
});
}
2.Giving a prompt to turn on GPS
Controller
if (window.cordova) {
cordova.plugins.diagnostic.switchToLocationSettings();
}
I think it is easier when using ngCordova plugin to detect GPS status. You can try this two plugins:
watch
function and error callback. You should use that for checking.$cordovaGeolocation
but has battery-saving
feature.Try with this
let optionsGPS = {timeout: 4000, enableHighAccuracy: true};
Geolocation.getCurrentPosition(optionsGPS).then((result) => {
this.loadMap(result.coords.latitude, result.coords.longitude);
}).catch((err) => {
let alert = this.alertCtrl.create({
title: 'Error on GPS',
subTitle: 'You need active the GPS',
buttons: ['Ok']
});
alert.present();
});
I had the same problem, and this it worked for me
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With