antoine dieval
antoine dieval

Reputation: 38

Ionic 3 How to know if GPS is enable

I'm using Ionic 3 and in a page in need to follow the position of the user ( only latitude and longitude ). How can I know if GPS is enable or not and how can I know if the user disable GPS through the android control panel ?

This is my code :

this.geolocation.watchPosition({enableHighAccuracy : true,maximumAge:0}).filter((p: any) => p.code === undefined).subscribe((position: Geoposition)=>{
    this.latitude = position.coords.latitude;
    this.longitude = position.coords.longitude;  
});

Upvotes: 0

Views: 842

Answers (1)

Parvej Mallick
Parvej Mallick

Reputation: 465

use android permissions https://ionicframework.com/docs/native/android-permissions/

this.geolocation.watchPosition({enableHighAccuracy : true,maximumAge:0}).filter((p: any) => p.code === undefined).subscribe((position: Geoposition)=>{
   this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.ACCESS_COARSE_LOCATION).then(
     result => console.log('Has permission?',result.hasPermission),
     err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.ACCESS_COARSE_LOCATION)
   );
   this.latitude = position.coords.latitude;
   this.longitude = position.coords.longitude;  
});

Upvotes: 2

Related Questions