@karabillie wrote:
Hi. I am having a toggle button. When I press it, I want to access the location of the device. If the user denies the location permissions, then I want to show a text that says: Permissions are denied. Please go to Settings to allow them. What is more, I want to uncheck the toggle button when I denied the permissions. What am I missing?
My function:
city = { isActive: false, cityName: '', countryCode: '', }; geoLatitude: number; geoLongitude: number; isActive: boolean; isLocationEnabled = false; locationSupported: boolean; appAuthorized: boolean; locationPermissionsDenied: boolean; openNativeLocationSettings; getGeoLocation() { this.platform.ready().then(() => { this.diagnostic.isLocationEnabled().then((res) => { alert('Is Location Enabled? ' + res); this.locationSupported = res; if (this.locationSupported) { this.diagnostic.getLocationAuthorizationStatus().then((status) => { alert(status); }) .catch((error) => { alert(error); }), this.diagnostic.isLocationAvailable().then((authorized) => { this.appAuthorized = authorized; if (!this.appAuthorized) { this.locationPermissionsDenied = true; this.openNativeLocationSettings = this.openNativeSettings.open('locations'); } }).catch((error) => { alert(error); }) } }).catch((err) => { alert(err); }); }); this.geolocation.getCurrentPosition({ maximumAge: 3000, timeout: 5000, enableHighAccuracy: true }).then((resp: any) => { this.geoLatitude = resp.coords.latitude; this.geoLongitude = resp.coords.longitude; console.log(this.isActive); const city = { isActive: this.isActive, latitude: this.geoLatitude, longitude: this.geoLongitude }; }).catch((error) => { console.log('Error getting location', error); }); }
My component view:
<ion-content fullscreen> <ion-list lines="full"> <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 to access location *HERE*. </ion-label> </ion-col> </ion-row> </ion-grid> </ion-content>
Posts: 1
Participants: 1