I am new to Ionic and trying to come up with a good way to check if Geolocation is available before loading my app data.
I am trying to go with the following but the setTimeout does not appear to run after ten seconds.
import { Component } from '@angular/core';
import { Diagnostic, Geolocation } from 'ionic-native';
import { NavController } from 'ionic-angular';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
lat: number;
lng: number;
constructor(public navCtrl: NavController) {
}
gpsReady() : boolean {
if( Diagnostic.isLocationAvailable() && Diagnostic.isLocationEnabled() && Diagnostic.isLocationAuthorized() ) {
return true;
} else {
return false;
}
}
locationReady() : boolean {
if(this.lat && this.lng) {
return true;
} else {
return false;
}
}
initiate() {
if(this.gpsReady()) {
Geolocation.getCurrentPosition().then((res) => {
this.lat = res.coords.latitude;
this.lng = res.coords.longitude;
this.doStuff();
}).catch((error) => {
this.showToast('Could not get GPS position data. Try enabling GPS and restart the app.');
});
} else {
// ask user to turn on GPS
}
}
ngOnInit() {
this.initiate();
}
doStuff() {
if( this.locationReady() ) {
// awesome app stuff
} else {
// wait 10 secs to se if GPS has fix
setTimeout(function(){ this.initiate(); }, 10000);
}
}
}
Is there a “standard Ionic way” of ensuring Geolocation before continuing to load app data?