Hi everyone. I want to show a message to user when he denies the permission of using location access. How can I do it? I somehow miss it somewhere.
geoLatitude: number;
geoLongitude: number;
isActive: boolean;
isLocationEnabled = false;
locationSupported: boolean;
locationPermissionsDenied: boolean;
authorizedLocation: boolean;
constructor(private location: Location, private httpService: HttpService, private geolocation: Geolocation,
private formBuilder: FormBuilder, private diagnostic: Diagnostic, public alertController: AlertController,
private platform: Platform, private openNativeSettings: OpenNativeSettings) { }
getGeoLocation() {
this.geolocation.getCurrentPosition({ maximumAge: 3000, timeout: 5000, enableHighAccuracy: true }).then((resp: any) => {
this.locationPermissionsDenied = false;
this.geoLatitude = resp.coords.latitude;
this.geoLongitude = resp.coords.longitude;
const city = {
isActive: this.isActive,
latitude: this.geoLatitude,
longitude: this.geoLongitude
};
console.log(this.geoLatitude);
this.httpService.changeIsActive(this.isActive);
this.httpService.changeCity(city);
}).catch((error) => {
alert('Error getting location ' + JSON.stringify(error));
});
}
controlPermissions() {
this.diagnostic.isLocationEnabled().then((res) => {
console.log(res);
this.locationSupported = res;
if (this.locationSupported) {
this.diagnostic.getLocationAuthorizationStatus().then((status) => {
console.log(status);
})
.catch((err) => {
alert(JSON.stringify(err));
}),
this.diagnostic.isLocationAvailable().then((authorized) => {
console.log(authorized);
this.authorizedLocation = authorized;
if (!this.authorizedLocation) {
this.locationPermissionsDenied = true;
this.isActive = false;
}
}).catch((err) => {
alert(JSON.stringify(err));
});
}
}).catch((err) => {
alert(JSON.stringify(err));
});
}
In my component view:
<ion-grid class="geoGrid">
<ion-row justify-content-center align-items-center>
<ion-col>
<ion-label position="stacked" class="geoLabel"
>Use current location</ion-label
>
</ion-col>
<ion-col class="geoToggle">
<ion-item lines="none">
<ion-toggle
slot="start"
name="blueberry"
[(ngModel)]="isActive"
(ionChange)="getGeoLocation($event)"
></ion-toggle>
</ion-item>
</ion-col>
</ion-row>
<ion-row *ngIf="locationPermissionsDenied">
<ion-col>
<ion-label position="stacked" class="geoLabel">
Location Permissions are denied. Please enable them in
<div
class="nativeSettings"
(click)="navigateToNativeSettings()"
tappable
>
Settings
</div>
to access location.
</ion-label>
</ion-col>
</ion-row>
</ion-grid>