Skip to content Skip to sidebar Skip to footer

Ionic Display Error If Gps Is Turned Off

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 no

Solution 1:

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();
}

Solution 2:

I think it is easier when using ngCordova plugin to detect GPS status. You can try this two plugins:

  1. $cordovaGeolocation:
    • This plugin using Wifi/3G/GPS to detect current position.
    • It also has watch function and error callback. You should use that for checking.
  2. Or $cordovaBackgroundGeolocation:
    • This works same as $cordovaGeolocation but has battery-saving feature.
  3. For prompt message, I think the only way is do it through plugin written by Java code. Check out Introduction to custom Cordova plugin development for more details.

Solution 3:

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

Post a Comment for "Ionic Display Error If Gps Is Turned Off"