Angular X秒后应用程序无法获取当前位置时使用的显示消息
我正在做一个ionic3和angular 4项目,有时需要你获取用户当前位置。有时,互联网速度可能会很慢,很难获得用户的当前位置。 我想实现的是,30秒后,如果应用程序无法获得当前位置,应该向用户弹出警报,告诉他/她再试一次Angular X秒后应用程序无法获取当前位置时使用的显示消息,angular,cordova,typescript,ionic3,Angular,Cordova,Typescript,Ionic3,我正在做一个ionic3和angular 4项目,有时需要你获取用户当前位置。有时,互联网速度可能会很慢,很难获得用户的当前位置。 我想实现的是,30秒后,如果应用程序无法获得当前位置,应该向用户弹出警报,告诉他/她再试一次 ionViewDidLoad() { let loader = this.loadingCtrl.create({ content : 'Checking Position...' }) loader.present().then(()
ionViewDidLoad() {
let loader = this.loadingCtrl.create({
content : 'Checking Position...'
})
loader.present().then(()=>{
this.geolocation.getCurrentPosition({
enableHighAccuracy:true,
timeout: 3000
}).then((resp) => {
this.userlng = resp.coords.longitude;
this.userlat = resp.coords.latitude;
console.log('latitude '+this.userlat+ ' longitude '+this.userlng);
})
})
}
使用Angular的rxjs库中的timeout和catchError函数。这基本上设置了30秒的超时时间,如果未获取位置,则抛出超时错误
ionViewDidLoad() {
let loader = this.loadingCtrl.create({
content : 'Checking Position...'
})
loader.present().then(()=>{
this.geolocation.getCurrentPosition({
enableHighAccuracy:true,
timeout: 3000
}).then((resp) => {
this.userlng = resp.coords.longitude;
this.userlat = resp.coords.latitude;
console.log('latitude '+this.userlat+ ' longitude '+this.userlng);
}).pipe(timeout(30000),
catchError(err => {
alert(" " + err);
throw("Error: "+ err);
}))
})
}
从rxjs导入捕获错误和超时