Get Location Mode in ionic 3
Checks whether device location mode features are enabled or disable available to the app
How is it work ?
Please follow these steps.
Step 1 : Install the Cordova and Ionic Native plugins
$ ionic cordova plugin add cordova.plugins.diagnostic
$ npm install --save @ionic-native/diagnostic
Step 2 : Add this plugin to your app's module
app.module.ts
import { Diagnostic } from '@ionic-native/diagnostic';
or
providers: [
StatusBar,
SplashScreen,Diagnostic,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
Step 3 : Add this code in home.ts
// In Header
import { Diagnostic } from '@ionic-native/diagnostic';
//In Constructor
private diagnostic: Diagnostic
// this code use to get location mode
this.diagnostic.getLocationMode()
.then((state) => {
console.log(state)
if (state == this.diagnostic.locationMode.DEVICE_ONLY||state == this.diagnostic.locationMode.BATTERY_SAVING||state == this.diagnostic.locationMode.HIGH_ACCURACY){
this.messageToast("Location is On")
}else if (state == this.diagnostic.locationMode.LOCATION_OFF){
this.messageToast("Location is Off")
} else {
this.messageToast("Location is Off")
}
}).catch(e => console.error(e));
Also Learn this
Battery saving = network triangulation and Wifi network IDs (low accuracy)
Device only = GPS hardware only (high accuracy)
High accuracy = GPS hardware, network triangulation and Wifi network IDs (high and low accuracy)
Get a full code
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { Diagnostic } from '@ionic-native/diagnostic';
@NgModule({
declarations: [
MyApp,
HomePage
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage
],
providers: [
StatusBar,
SplashScreen,Diagnostic,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
home.ts
import { Component } from '@angular/core';
import { NavController,ToastController } from 'ionic-angular';
import { Diagnostic } from '@ionic-native/diagnostic';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(public navCtrl: NavController,
private diagnostic: Diagnostic,
private toastCtrl: ToastController,) {
}
getsLocationMode(){
this.diagnostic.getLocationMode()
.then((state) => {
console.log(state)
if (state == this.diagnostic.locationMode.DEVICE_ONLY||state == this.diagnostic.locationMode.BATTERY_SAVING||state == this.diagnostic.locationMode.HIGH_ACCURACY){
this.messageToast("Location is On")
}else if (state == this.diagnostic.locationMode.LOCATION_OFF){
this.messageToast("Location is Off")
} else {
this.messageToast("Location is Off")
}
}).catch(e => console.error(e));
}
//--------------Toast Start --------------
messageToast(message) {
let toast = this.toastCtrl.create({
message: message,
duration: 4000,
position: 'middle'
});
toast.onDidDismiss(() => {
console.log('Dismissed toast');
});
toast.present();
}
//--------------Toast End --------------
}
home.html
<ion-header>
<ion-navbar>
<ion-title>
JawabAdda
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<button (click)="getsLocationMode()" ion-button color="primary" block>
Location Mode
</button>
</ion-content>