cordova - 如何使用科尔多瓦 LocationAccuracy 等待使用 aprove 的地理位置?
问题描述
我LocationAccuracy
在 Ionic 上使用,当应用程序在没有启用位置功能的情况下打开时出现问题,应用程序请求权限正确但之后他什么也没做
查看应用程序是否获得许可的代码
autorizacoes(){
this.diagnostic.isLocationEnabled().then(
(resp)=>{
if(resp){
this.on_load()
}else{
this.diagnostic.registerLocationStateChangeHandler(()=>{this.on_load()})
this.request_location()
}
}
).catch(err=>{
this.alertCtrl.create({
title: 'Erro',
buttons: [{ text: 'ok' }],
subTitle: 'Ative o gps para prosseguir'
}).present();
})
}
请求代码
request_location(){
this.carregando = true
this.locationAccuracy.canRequest().then((canRequest: boolean) => {
if (canRequest) {
this.status = 'pode pedir' this.locationAccuracy.request(this.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY).then(
() => {
this.status='vai esperar'
this.on_load()
},
).catch(err=>{
this.status = 'deu erro'
this.alertCtrl.create({
title: 'Erro',
buttons: [{ text: 'ok' }],
subTitle: 'Ative o gps para prosseguir'
}).present();
this.request_location()
})
}
else{
this.request_location()
}
});
}
最后是我要在开启位置后运行的代码
on_load() {
this.geolocation.getCurrentPosition().then((resp) => {
this.pegar_orcamentos_diponiveis(this.raio, { lat: resp.coords.latitude, lng: resp.coords.longitude });
console.log(this.orcamentos.length)
setTimeout(()=>{
//this dont run
this.autorizacoes()
},30000)
}).catch((error) => {
console.log('Error getting location', error);
this.autorizacoes()
});
setTimeout(() => {
this run
this.status = 'end of time'
this.autorizacoes()
}, 30000)
}
我放了一个timeout
只是为了测试代码的去向。
解决方案
我通过在发出请求后重新加载页面解决了这个问题,我使用了 window.location.reload()。不是正确的方法,但有效
推荐阅读
- django - 无法在 django 应用程序中使用 docker 运行 Celery 和 celery beat(无法加载 celery 应用程序)
- python - 我想反转堆栈,但我不知道如何使用递归来反转这个......如何在不使用递归的情况下反转堆栈
- javascript - 倒计时javascript的自定义事件
- python - Django 模板无法加载
- arrays - 对象数组的访问时间是否为 O(1)?
- java - 在 java 类文件中发现了一个未指定的 JVM 字节码 (0xe2)
- amazon-web-services - AWS EKS Fargate 和节点组
- java - 为控制流实现 equals() 好吗?
- azure-devops - 如何通过重新排列我的 azure-pipelines.yml 来纠正我的 azure 部署问题?
- node.js - 猫鼬如何推送到数组