Search code examples
cordovagpsphonegap-plugins

phonegap plugin to check gps is on or off


I have been trying to find out a plugin which will trigger if the gps is turned on or off during the app is running. So I start the app and the gps is turned off but later I turn it on.

I have tried

cordova.plugins.diagnostic

and

cordova-plugin-geolocation

but they don't put a sort of binding. In the start I can look if it is on or off but as the app is running I can't


Solution

  • cordova-diagnostic-plugin provides registerLocationStateChangeHandler() which allows you to register a callback function to be invoked when a change in Location state occurs - I believe this should satisfy your requirement of "a plugin which will trigger if the gps is turned on or off during the app is running":

    $ cordova plugin add cordova.plugins.diagnostic
    

    then

    cordova.plugins.diagnostic.registerLocationStateChangeHandler(function(state){
        if((device.platform === "Android" && state !== cordova.plugins.diagnostic.locationMode.LOCATION_OFF)
            || (device.platform === "iOS) && ( state === cordova.plugins.diagnostic.permissionStatus.GRANTED
                || state === cordova.plugins.diagnostic.permissionStatus.GRANTED_WHEN_IN_USE
        ))){
            console.log("Location is available");
        }
    });