How to programmatically enable and disable GPS in Ionic Framework


#1

How to programmatically enable and disable GPS in Ionic Framework


#2

Hi @harry_monu

Check this conversation, this is very helpful for your requirement.


#3

thanks for reply

if (window.cordova) {
  cordova.plugins.diagnostic.isGpsLocationEnabled(
                function(e) {
                    if (e){
                      alert("location on")
                     
                    }   
                    else {
                      alert("Location Not Turned ON");
                      cordova.plugins.diagnostic.switchToLocationSettings();
                    }
                },
                function(e) {
                    alert('Error ' + e);
                }
            );
        }

i have this code for enable the location and its working very fine .but i wanted to enable my location via my mobile app


#4
$rootScope.turnOnGps = function () {
  if (ionic.Platform.isAndroid())
    cordova.plugins.locationAccuracy.request(onRequestSuccess, onRequestFailure, cordova.plugins.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY);
};

function onRequestSuccess(success){
  $rootScope.isGpsEnabled = true;
  //getLocation();
  console.log("Successfully requested accuracy: "+success.message);
}

function onRequestFailure(error){
  $rootScope.isGpsEnabled = false;
  $ionicLoading.hide();
  console.error("Accuracy request failed: error code="+error.code+"; error message="+error.message);
  if(error.code !== cordova.plugins.locationAccuracy.ERROR_USER_DISAGREED){
    if(window.confirm("Failed to automatically set Location Mode to 'High Accuracy'. Would you like to switch to the Location Settings page and do this manually?")){
      cordova.plugins.diagnostic.switchToLocationSettings();
    }
  }
}

you can call turnOnGps method from button.

plugin : https://github.com/dpa99c/cordova-plugin-request-location-accuracy

let me know is it working or not… :slight_smile:


#5

@Sandy1 Thanks its working very fine :slight_smile:
Happy Coding :slight_smile:


#6

:slight_smile: That’s great

I used same scenario in my app