The Metafence
ScaleUp! Community, previously we wrote about geolocation, geofences, how to acquire locations for your app, defining the process of geocoding and, most recently, performing geocoding. This article will focus on a neat trick, called the metafence, that your mobile apps can use in lieu of significant location change events.
What is Significant Location Change
Significant location change is an API that iOS mobile apps can use to detect user location changes. It is built into iOS and meant to be power-friendly and roughly accurate. Roughly accurate in the prior sentence means that it is not as accurate as GPS based location updates. Furthermore, when using significant location change, your app will get less location change events than when using standard location services. However, for many apps, significant location change is detailed enough. Other apps, which use more accurate location change events for certain workflows, also depend on significant location changes. A typical use case for significant location change is to trigger your app to reload a set of location specific data – such as nearby geofences.
The Problems with Significant Location Change
The first problem with significant location change is that it only exists for iOS. Your mobile app will target users with both iOS and Android devices so, if you use significant location change for iOS, you will need to use a different approach for Android.
The second problem with significant location change is that your app has no control over how it works, and, more importantly, when you will receive events.
The Metafence
The metafence was a concept that we came up with for an app that leveraged geofences. The app would load from the server, and add for entry and exit monitoring, the 20 geofences nearest to the current location of the user. As the user moved around, the 20 monitored geofences would need to be refreshed. The solution was, in the 20 geofences returned to the apps by the server, to include 19 real geofences and 1 metafence, which had its radius calculated so that it encompassed all of the 19 real geofences. The mobile app can use exit event events on this metafence to determine the optimal time to load a new set of nearby geofences from the server.
Details
The metafence is created by computing the haversine distance in meters between the user’s current location (latitude, longitude) and the center of the geofence most distant from their current location (latitude, longitude).
We then reduce that computed haversine distance by 25% so that we reload nearby fences as the user approaches the most distant fence, not when they actually reach this most distant fence.
Lastly, we check the computed and reduced haversine distance in meters against a maximum metafence fence radius of 50,000 meters (50 km) that we have defined. If the computed and reduced haversine distance is greater than 50 km, we use 50 km as our metafence radius. Otherwise, we use the computed and reduced haversine distance in meters from the user’s current location to the center of the most distant geofence as our metafence radius.
Up next
Loading an infinite number of geofences