Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To get location of device after specific intervals when phonegap application is running in background through Geolocation API

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?

like image 688
Asif Mehmood Avatar asked Nov 08 '22 14:11

Asif Mehmood


1 Answers

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!

like image 94
N-ate Avatar answered Nov 24 '22 00:11

N-ate