Tracking multiple (20+) locations with iOS geofencing

silentser picture silentser · Jan 9, 2013 · Viewed 12.1k times · Source

An iOS application uses the geofencing for notifying the user about predefined nearby locations. The application is allowed to miss some location (the user is not getting a notification about a nearby location), but it is desirable to keep the missing rate low.

One way to implement this would be to start monitoring for significant change locations with startMonitoringSignificantLocationChanges and each time the "location change" event is fired, look for locations within, let say, 500m radius of the reported location.

What worries me is the requirement to perform the query for the nearby regions each time the significant location change occurs and it impact on the battery.

The other way to do it would be to register the locations with startMonitoringForRegion but Apple has put a (reasonable) limitation on the number of simultaneously tracked regions which is 20 and we have significantly more than 20 locations. So some sort of dynamic updating of the tracked regions is required but I am still unsure what is the best way to do it.

Any ideas on how can it be done so that it keeps the battery consumption low but also has the low missing rate for locations?

Answer

silentser picture silentser · Mar 8, 2013

Since there was not much activity on the question I will describe how we are currently solving this problem.

We tied the reloading of the new regions to significant location change (SLC) events. When an SLC takes place, we check for 20 neighbouring regions that should be "geofenced". To find the 20 closest regions we are simply approximating 1'' of the latitude and longitude according to the following formulae:

Latitude: 1 deg = 110.54 km

Longitude: 1 deg = 111.320 * cos(latitude) km

and just check the bounding square of the current position of the device for the centers of the monitored regions (see: Simple calculations for working with lat/lon + km distance?)

So, for example, if (10N,10E) is the current location of the device we start with the bounding square with vertices at (10-1',10-1'), (X-10',10+1'), (10+1',10+1'), (10+1',10-1') (at latitude (10N,10E) one latitude/longitude minute approximates 1,85 km).

If there are 20 (or almost 20) - we register them for the geofencing and wait for the next SCL. If less/more, just increase/decrease the size of the bounding rectangle and repeat the search.

You can tweak this search algorithm for a better performance, but the one described here will already do the job.