I have developed an application in PhoneGap that is tracking your traveled route through geolocation API, I am getting the current position of the device through navigator.geolocation.getCurrentPosition(onSuccess, onError);
and updating the polyline by getting the position after regular intervals through navigator.geolocation.watchPosition(function(position)
But now the problem is it's not working in background i.e (when I open another application and it starts running in background ) it doesn't update the polyline mean not triggering this navigator.geolocation.watchPosition(function(position)
function so I tried to run the app through Cordova-plugin-background-mode
this plugin its start running the app in the background and again that wasn't triggering the navigator.geolocation.watchPosition(function(position)
function means this wasn't required i.e (there wasn't any need to install the plugin to run the app in background mode). So please help me to solve this problem of getting the position in background mode. And if I use this Cordova-plugin-mauron85-background-geolocation
plugin how I can get a position as I was getting through watch position? and what to do if the app is running in the foreground? do I have to use the watchPosition
function or something else?
You may want to dig into this location plugin. They seem to think they've solved the problem.
A quick fiddle and a test on phones I have access to show this is not a phonegap issue. It is an OS issue. Most phones have been optimized to suspend background activity, especially web browser activity. It is probably best to tie into native events to get the behavior you want.
navigator.geolocation.watchPosition( positionChanged, err, options );
I did notice that phones keep all connections open even for background tasks. You may be able to game the situation by making an ajax call to a server that doesn't refuse, but fails to respond, add a timeout handler, and setup the interval you want, in the ajax timeout handler check the location and then setup an new call.
Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With