gpt4 book ai didi

angular - Ionic 3 - 检查位置是否已启用

转载 作者:太空狗 更新时间:2023-10-29 18:28:52 27 4
gpt4 key购买 nike

我正在尝试将几件事合二为一。我要做的第一件事是获取设备位置。在android上,它提示授予应用程序访问位置的权限,这一切都很好。我正在尝试检查设备是否打开了定位功能,如果没有,我会提示用户使用 switchToLocationSettings() 进行此操作。 .我遇到的问题是当我添加下面的代码时,它不会从 this._DIAGNOSTIC.isLocationEnabled().then((isEnabled) => { 行运行我一遍又一遍地按下按钮,什么也没有。请如何提示用户打开设备位置,然后使用 switchToLocationSettings() 导航当它打开时,我可以获得用户位置并返回它。谢谢

html**

<ion-item>
<ion-label color="primary" stacked>Enter your zip code</ion-label>
<button item-right ion-button (click)="getGeolocation()" ion-button clear color="dark" type="button" item-right large>
<ion-icon color="dark" name="locate" md="md-locate" color="primary"></ion-icon>
</button>
</ion-item>

import { Component } from '@angular/core';
import { NavController, Platform, LoadingController, AlertController } from 'ionic-angular';
import { Geolocation, Geoposition, GeolocationOptions } from '@ionic-native/geolocation';
import { Diagnostic } from '@ionic-native/diagnostic';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
geolocationOptions: GeolocationOptions;
loading : any;
location : Object;

constructor(
private alertCtrl : AlertController,
private _DIAGNOSTIC: Diagnostic,
private _GEO : Geolocation,
private loadingCtrl: LoadingController,
public navCtrl: NavController,
private _PLATFORM : Platform,
public restProvider: RestProvider
) {
}

async getDeviceCurrentPosition() {
await this._PLATFORM.ready();
this.geolocationOptions = {
enableHighAccuracy: true
}
await this._GEO.getCurrentPosition(this.geolocationOptions).then((loc : any) =>
{
console.log("getting position");
//check if location is turn on
this.loading = this.loadingCtrl.create({
spinner: 'crescent',
content: 'Loading Please Wait...',
});
this.loading.present();
this._DIAGNOSTIC.isLocationEnabled().then((isEnabled) => {
if(!isEnabled && (this._PLATFORM.is('android') || this._PLATFORM.is('ios'))){
let confirm = this.alertCtrl.create({
title: '<b>Location</b>',
message: "For best results, turn on devices location.",
buttons: [
{
text: 'cancel',
role: 'Cancel',
handler: () => {
this.closeLoader();
}
},
{
text: 'Ok',
handler: () => {
this._DIAGNOSTIC.switchToLocationSettings();
}
}
]
});
confirm.present();
}
})
.catch((error : any) =>
{
this.closeLoader();
console.log('Location Not enabled', error);
});

})
.catch((error : any) =>
{
console.log('Error getting location', error);
});
}


private closeLoader(){
this.loading.dismiss();
}
}

最佳答案

您应该先检查位置是否启用,然后再获取当前位置。您正在以相反的方式进行操作。 getCurrentPosition 方法返回一个Promise,它根据设备的位置进行解析。如果该位置未启用,它不会返回任何 Promise。所以代码将是,

this._DIAGNOSTIC.isLocationEnabled().then((isEnabled) => {
if(!isEnabled && this._PLATFORM.is('cordova')){
//handle confirmation window code here and then call switchToLocationSettings
this._DIAGNOSTIC.switchToLocationSettings();
}
else{
this._GEO.getCurrentPosition(this.geolocationOptions).then((loc : any) =>
{
//Your logic here
}
)
}
})

关于angular - Ionic 3 - 检查位置是否已启用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51851422/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com