Button click to turn on mobile gps

hi guys need a help, how to turn on mobile gps on button click…
i need to turn on gps if gps turned off…

here is my code:

this.geolocation.getCurrentPosition().then((resp) => {
      this.lat = resp.coords.latitude;
      this.lng = resp.coords.longitude;
      this.latlong = resp.coords.latitude + ',' + resp.coords.longitude;
      console.log('loaction: ' + this.latlong)
}).catch((error) => {
  console.log('Error getting location', error);
});

this code working fine if gps is on. and not working if gps is off as expected. now i need to show an alert to turn on gps if gps is off like this

Just add an Alert Controller.


Alert Controller

presentConfirm() {
  let alert = this.alertCtrl.create({
    title: 'Locations = off!',
    message: 'Location needs to be turned on. Turn on now ?',
    buttons: [
      {
        text: 'Cancel',
        role: 'cancel',
        handler: () => {
          console.log('Cancel clicked');
        }
      },
      {
        text: 'Ok',
        handler: () => {
          //!ADD YOUR CODE HERE!
          console.log('Ok clicked');
        }
      }
    ]
  });
  alert.present();
}

Your Code

this.geolocation.getCurrentPosition().then((resp) => {
      this.lat = resp.coords.latitude;
      this.lng = resp.coords.longitude;
      this.latlong = resp.coords.latitude + ',' + resp.coords.longitude;
      console.log('loaction: ' + this.latlong)
}).catch((error) => {
  this.presentConfirm();
  console.log('Error getting location', error);
});

I’ve found a promising code here. I hope this is what you wanted.
Else download a demo application wich also uses location settings and look how they coded it. :slight_smile: