Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to track the device location (iOS and Android) device using Phonegap

I would like to know when a user arrives or leaves certain location. I was trying to do this using the wireless network (check for mobile devices), but I couldn't for several reasons.

1) I needed real time updates or every 1 - 5 min of the information about which devices are connected and which devices have just disconnected.

2) I had very high ping from my PC to my iPhone on the same network (still don't know why).

Now I want to do it using geolocation from a Phonegap application (running in the background) suspended on iOS or running in the background in Android.

Any help would be appreciated.

like image 646
amarruffo Avatar asked Jul 11 '13 02:07

amarruffo


1 Answers

Update 25 May 2019

My original answer below is 6 years old and out-of-date with respect to current mobile OS versions. For example partial wakelocks no longer work on modern Android versions.

Today my recommendation would be (if you have a serious commercial app and not a hobby project) to use the paid-for version of the cordova-background-geolocation plugin by Transistorsoft. The free version works for iOS but for Android a license is required which costs in the order of several hundred US dollars. However I think the price is worth it: in my 10+ years of experience in creating location-aware mobile apps, this has been the most sophisiticated and reliable solution I've encountered. For the cost of your license, you get access to the private repo which is continually updated and maintained to be compatible with new versions of Android & iOS.

If you're looking for a free/open-source solution, I would go with cordova-plugin-background-geolocation which is an open-source fork of the original plugin by Transistorsoft. However my experience with this plugin has been of mixed success; due to being free/open-source, it's not updated as frequently as the paid-for Transistorsoft plugin. I encountered problems due to new more stringent restrictions on background location in recent versions of Android which took a significant time to be resolved or have not been fixed at all (see the plugin's issue list).

Original answer (11 Jun 2013)

The first thing to say is that creating a Phonegap app that receives location updates while running in the background is entirely possible, but not trivial. I've done it myself and released apps on both the Android and iOS platforms.

If you need accurate and regular position updates, I'd suggest using the GPS receiver on the target devices. In Phonegap, you can do this setting the "highAccuracy" flag when requesting position updates. The watchPosition() function will deliver new position information as and when the device receives updates from the GPS receiver, so you use it something like this:

navigator.geolocation.watchPosition(successCallback, errorCallback, {
  enableHighAccuracy: true,
  timeout: 10000,
  maximumAge: 0
});

See the Phonegap geolocation API documentation for more details (note the permissions that are required to make this work on Android and iOS).

To keep your app running in the background on either Android or iOS you will need to setup their respective native development environments: Eclipse for Android, XCode for iOS. You will not be able to use Phonegap Build because custom settings are needed in both cases to make it work. See the Phonegap platform guides for how to do this.

To keep your app running in the background on Android, you either need to write a custom Android service, or you could do what I did and use a Phonegap plugin to acquire a "partial wakelock" (see here) to keep your app running in the background and receive position updates while the screen is off. Note that the original version of this plugin is out-of-date and doesn't work with more recent versions of Phonegap, and also doesn't support partial wakelocks. However, I've updated and extended it for my own use: you can find the source code for it in my answer to this question.

To keep your app running in the background on iOS, you need to do things slightly differently; you don't need a plugin, just a project setting.

You can either manually edit the project .plist and add the key “UIBackgroundModes” key with a value of “location” or, with your project open in XCode, add the "Required Background Modes" key with a value of "App registers for location updates". This will cause iOS to trigger the JS callback function you have registered with watchPosition() each time a location update is received. See here for more about iOS project keys and background modes.

Hope this helps!

like image 175
DaveAlden Avatar answered Sep 23 '22 03:09

DaveAlden