GPS coordinates in background in cordova app
So I currently have an app that addresses all the issues you listed above and here's the plugin I'm using:
https://github.com/mauron85/cordova-plugin-background-geolocation
The plugin makes use of watchPosition() not getCurrentPosition() as this one takes too long to constantly ping the device and consumes more battery power.
This will definitely work for Android & iOS but IMHO it plays nicer with Android than the latter, as far as precision and the keep alive functionality.
I got it into Google Play no problem, Apple does allow this plugin, there are a number of apps using this plugin in the Apple store but Apple will probably initially reject it and ask the apps intention of background usage, you will then have to make an appeal as for what the app is doing in the background and make sure that it doesn't run indefinitely (this was my experience).
a. You're going to also want to make sure you point out to the Apple peeps that there is a way for the User to turn the background geolocation tracking off. I'm assuming there is? That's their main issue with the usage of the plugin.
Good luck.
This plugin has a great guide for how to use a meteor server and cordova to do what you need:
zeroasterisk/meteor-cordova-geolocation-background
It configures automatically with both android and iOS. For windows phone I don't know.
- Meteor configures this Plugin in Cordova (you have to configure)
- Meteor configures this Plugin in Cordova (you have to configure)
- Meteor can trigger the Background service to get Geolocation (GPS) details
- The Cordova Background service periodically POSTs it's data to the Meteor server (not to the client Cordova instance)
- Meteor Server can update a Collection (or anything else)
- Meteor Client syncs with the server
I encountered the exact same issue, using the same plugins that you used. It turns out that it's a device permissions issue.
The cordova-plugin-geolocation plugin does not have the permission to run in the background, and more importantly it doesn't have the permission to track GPS while in the background. While the cordova-plugin-background-mode plugin does allow you to execute code in the background, you won't be able to get GPS coordinates while in the background.
The solution is to add a plugin that supports getting the location data while the app in the background. There are several plugins that support this, including the mauron85/cordova-plugin-background-geolocation plugin (as suggested previously). An alternative is to use the cordova-custom-config plugin which allows you to specify your own custom permissions. Really you can add any plugin, as long as it supports background location services. No additional code is required.
You can verify that you have the correct permissions, by opening the app settings (in iOS) and confirming that the "Always" option is available.