Ionic 3 - 检查位置是否已启用
Ionic 3 - check if location is enabled
我正在尝试将几件事合二为一。我要做的第一件事是获取设备位置。在android,它提示授予应用程序访问位置的权限,这一切都很好。
我正在尝试检查设备是否打开了定位功能,如果没有,我想提示用户使用 switchToLocationSettings()
进行此操作。
我遇到的问题是,当我添加下面的代码时,运行 行中没有 this._DIAGNOSTIC.isLocationEnabled().then((isEnabled) => {
我一遍又一遍地按下按钮,什么也没有。请如何提示用户打开设备位置,然后使用 switchToLocationSettings()
导航,当它打开时,我可以获得用户位置并 return 它。谢谢
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>
ts
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 方法 return 是一个根据设备位置解析的 Promise。如果该位置未启用,它不会 return 任何 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
}
)
}
})
我正在尝试将几件事合二为一。我要做的第一件事是获取设备位置。在android,它提示授予应用程序访问位置的权限,这一切都很好。
我正在尝试检查设备是否打开了定位功能,如果没有,我想提示用户使用 switchToLocationSettings()
进行此操作。
我遇到的问题是,当我添加下面的代码时,运行 行中没有 this._DIAGNOSTIC.isLocationEnabled().then((isEnabled) => {
我一遍又一遍地按下按钮,什么也没有。请如何提示用户打开设备位置,然后使用 switchToLocationSettings()
导航,当它打开时,我可以获得用户位置并 return 它。谢谢
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>
ts
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 方法 return 是一个根据设备位置解析的 Promise。如果该位置未启用,它不会 return 任何 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
}
)
}
})