Press "Enter" to skip to content

Distance-based background location updates on iOS

In my previous article, I covered how we can get background time-based location updates in iOS. This time I want to talk about a distance-based approach, iOS has support for significant location updates which works well for getting updates when traveling long distances (>= 500 meters). But when short distance traveled updates are needed, a distance-based update approach is more reliable as we can request a one-time location update each time the specified distance is traveled.

In this article, I will show you how to get distance-based location updates by using iOS region monitoring. “Region monitoring (also known as geofencing) is a way for your app to be alerted when the user enters or exits a geographical region”.

Let’s Start

1. Add required permissions

Add location permissions and enable location updates background mode in Info.plist

2. Create an interface that represents our geolocation service

Create an interface that will represent the API abstraction for our geolocation distance-based service.

3. GeolocationService implementation

Implement the interface above by using CLLocationManager Region Monitoring.

In the StartUpdating method, we subscribe to the CLLocationManager LocationsUpdate event and RegionLeft event. Also, we request a location update.

In the LocationsUpdate method, we start monitoring a region that represents a circular area where is contained our current location. The region center point will be the new location and the radius will be the distance that was specified as a parameter when calling the StartUpdating method which is the distance to be traveled before getting a location update (Recommend minimum distance should be 150 meters).

In the RegionLeft event, we request a new location update. This event will be triggered every time the device leaves the region.

Now, whenever we get a new location update our geofence region center point will be updated which will allow us to get a location update every time device leaves the updated region. Since the geofence region is circular this means that we can travel the specified distance in any direction it will fire the RegionLeft event.

4. Test it!

For testing this service I created a simple custom map control, a ViewModel, and ContentPage in Xamarin Forms.

As you can see in the GIF above, every time the position of the big blue circle is updated is when we get a location update. You might notice a slight delay because the RegionLeft event doesn’t fire right away, it takes a couple of seconds to fire.

Final Thoughts

  • If you are monitoring a distance traveled greater than or equal to 500 meters might be better to use significant location monitoring.
  • You can use a mixed approach if it suits better for your use case by combining distance-based with time-based location updates.
  • Since region monitoring is being used for this approach it will work even when the application is closed. Region monitoring is one of the location services that relaunches the app after it has been terminated, you can find more information here.

You can check the full source code here.

Happy Location Updates!