4

I am creating a mobile app using node, angular, ionic and cordova, which should be compatible with android version 4.0.3 and higher. I want the app to pop up a notification if GPS is disabled by the user. And also the notification should have options to cancel and direct to GPS settings for the user to switch GPS on manually.

To do this I tried following npm plugins, but none of them worked for me. When I tried them, only the splashscreen showed up and nothing else.

  • cordova.plugins.diagnostic
  • cordova-plugin-fastrde-checkgps
  • cordova-plugin-android-gpsdetect

This is the code I tried using cordova.plugins.diagnostic plugin.

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {

   if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
     cordova.plugins.Keyboard.disableScroll(true);
   }
   if (window.StatusBar) {
     StatusBar.styleDefault();
   }

   cordova.plugins.diagnostic.isGpsLocationEnabled(function(enabled){
     alert("GPS location is " + (enabled ? "enabled" : "disabled"));
   }, function(error){
     alert("error");
  });
})

What am I doing wrong here? I followed this in my code. Can someone please tell me how to get this done?

Community
  • 1
  • 1
tsam
  • 51
  • 3

2 Answers2

1

Problem solved. I have added the following part of code to the wrong place.

cordova.plugins.diagnostic.isGpsLocationEnabled(function(enabled){
   alert("GPS location is " + (enabled ? "enabled" : "disabled"));
}, function(error){
   alert("error");
});

It shouldn't be called within $ionicPlatform.ready function, because the plugin is not defined before the device is fully ready I guess. I put it in one of my controllers and it worked.

tsam
  • 51
  • 3
0

There is a very nice cordova plugin for this. Not only it checks if location is turned on but also you can ask for different accuracy and turn them on without redirecting the user to his location settings.

cordova-plugin-request-location-accuracy

cordova.plugins.locationAccuracy.request(function(success){
  console.log("success");
},function(err){
   console.log("fail. Prompt will show up, turn on location serives or use precise location");
},cordova.plugins.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY)
Raj Nandan Sharma
  • 3,694
  • 3
  • 32
  • 42