Search code examples
angularjscordovaionic-frameworkcordova-plugins

Programmatically enable GPS of device when application opens in cordova


I have been working on cordova ionic app with angularJS frontend. Here when this app is getting launch the device should check for the status of the GPS whether it is on or not and if it is not on then make it on automatically. I didn't found any cordova event that will do this task. How can it be done programmatically at the time of app launch in deviceready event or with any other thing?


Solution

  • You could check the error code in PositionError parameter of geolocationError in your call to getCurrentPosition. I am guessing that it will be PositionError.PERMISSION_DENIED when gps is not enabled, and PositionError.POSITION_UNAVAILABLE or PositionError.TIMEOUT when gps is enabled but there are other issues.

    Note that this is platform dependent. You would probably have to write a contrived error message that says "Could not get the current position. Either GPS signals are weak or GPS has been switched off".

    One thing you can try is to call getCurrentPosition with an incredibly small timeout, say 1 ms. If it says permission denied, you can conclude that gps is disabled and if it times out, you can assume that gps is enabled. I do not have time to test this, you could probably edit this answer with result of your tests.

    Another thing you can try is to use the diagnostic phonegap plugin for android. You will have to make sure you use the plugins for the other platforms also, but they are all also mostly there.

    http://ngcordova.com/docs/plugins/geolocation/