Search code examples
androidvalidationgeolocationgpsionic2

How to check GPS is enable or not with Ionic2 (Geolocation)?


I use plugin cordova Geolocation for ionic 2 but I need to know if the GPS is enabled or disabled . I don't know how make this.

Please Help Me ;) Thanks!


Solution

  • The diagnostic plugin has your back with a lot of functionality to check for GPS mode, location status and more. Check the included link to know more about it.

    import { Diagnostic } from '@ionic-native/diagnostic';
    
    constructor(private diagnostic: Diagnostic) { }
    
    ...
    
    let successCallback = (isAvailable) => { console.log('Is available? ' + isAvailable); };
    let errorCallback = (e) => console.error(e);
    
    this.diagnostic.isLocationEnabled().then(successCallback).catch(errorCallback);
    
    // only android
    this.diagnostic.isGpsLocationEnabled().then(successCallback, errorCallback);
    

    You can also use the location-accuracy plugin which is lighter but it's missing other options available on the diagnostic plugin. I'm currently using this plugin to enable gps on android and to set the mode. I'll suggest to also look at the doc of this plugin.