Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check GPS is enable or not with Ionic2 (Geolocation)?

I use plugin cordova Geolocation for ionic 2 but I need to know if the GPS is enabled or disabled . I don't know how make this.

Please Help Me ;) Thanks!

like image 454
Ricardo Andres Rodriguez Gambo Avatar asked Mar 08 '23 18:03

Ricardo Andres Rodriguez Gambo


2 Answers

The diagnostic plugin has your back with a lot of functionality to check for GPS mode, location status and more. Check the included link to know more about it.

import { Diagnostic } from '@ionic-native/diagnostic';

constructor(private diagnostic: Diagnostic) { }

...

let successCallback = (isAvailable) => { console.log('Is available? ' + isAvailable); };
let errorCallback = (e) => console.error(e);

this.diagnostic.isLocationEnabled().then(successCallback).catch(errorCallback);

// only android
this.diagnostic.isGpsLocationEnabled().then(successCallback, errorCallback);

You can also use the location-accuracy plugin which is lighter but it's missing other options available on the diagnostic plugin. I'm currently using this plugin to enable gps on android and to set the mode. I'll suggest to also look at the doc of this plugin.

like image 134
Jean Guzman Avatar answered Mar 11 '23 08:03

Jean Guzman


I had good luck with this plugin http://devfanaticblog.com/background-geolocation-with-apache-cordova/ cordova plugin add cordova-plugin-mauron85-background-geolocation

Might be extreme overkill for what you need though. You could always just call getcurrentposition() and check if you get a valid result.

navigator.geolocation.getCurrentPosition(success[, error[, options]])

I am not certain how iPhones treat it, but Android can be downright evil now. Because the user will be prompted to give permission to their GPS if you ask to use it. If they choose no, then it could be enabled, but unavailable to you. You also can't easily tell what precision you are getting. You might be getting wifi or mobile location instead of GPS location.

This plugin covers all those scenarios, however, it has an odd bug that it doesn't work properly in the android emulator.

like image 41
Trevor Avatar answered Mar 11 '23 06:03

Trevor