Background geolocation

18 December 2018
Processing location update events
gps

Accessing user current location in the app is a common practice nowadays. Depending on the usage of the location data, a common use case is to present the data in a sorted by distance filter.

Say, a restaurant app, where you want to see the nearest restaurant first.

I have been fortunate to work on a complicated geolocation aware app, where I need to track the changes of the GPS location over time.

Think of it like a fitness app, or a more popular example will be the pokemon app.


The existing solution

While most app would be enough with a listener watching for the GPS location update, the one I am working on needs more.

Basically, it is a fitness app to track jogging pattern. It needs to work when the app is in background, and offline (for some of the time).

The current available location listener watchPosition seems to be great for the work, until it is not.


The problem

The app needs to work together with a geo-fencing ability to detect if a user is within an action zone, where it will trigger a log, and the network request (if network connection is good).

So the main challenge is to get it to work when the app is in background, and I want to be able to detect the event as soon as the user enters the zone.


Background geolocation to the rescue

In order to be able to achieve that, this package (https://github.com/mauron85/react-native-background-geolocation) has done incredible work the background location.

However it is not a silver bullet for all the problems out there, the app I wanted to build are meant for fitness and jogging, so there are a different configuration needed to be able to adapt to my use case.




Key takeaway

Couple the package with other features, such as drawing the jogging path on a map, show the geo-fencing zone on the map is more reliable and complete thanks to a more frequent location updates, event in the background.

It is advisable to create a couple of .gpx files to mock the location, otherwise the development with geolocation would be miserably slower.

More about this part in the future. Now that you know how to get location updates even app is in the background, you will be able to build a better app!