Article in Mobile category.
Geofencing to the Rescue
What is a geofence? [caption id="attachment_27917" align="aligncenter" width="339"] From: developer.android.com[/caption] A “geofence” is a virtual geographic boundary that enables software to trigger a response when…
What is a geofence?
A “geofence” is a virtual geographic boundary that enables software to trigger a response when a mobile device enters or leaves a particular area.
Why a geofence?
Ok so it’s a virtual geographic boundary, but why do I need to know about it as a developer? 🤔
Let’s discuss a couple of use cases where geofence comes to our rescue :
- Location-specific app behavior: This is the use case that I personally encountered in one of my applications, where a key feature was to sell ski passes to the user. On the home screen we wanted to show details like weather conditions, venue description, and venue maps—basically all the key details relevant to that particular venue as soon as the user enters its vicinity. If the user is not near any of the venues listed in the app, we wanted to show them a list of all venues instead. This is a tailor fit example for using geofences. All one needs to do is create a geofence for each venue in order to be notified of entry and exit events— it’s as simple as that! The whole user experience then becomes so personalized with very little development effort—that’s the beauty of it!
- Marketing: A lot of applications these days use tailor-made push notifications based on past user interactions with the app. Push notifications serve as a great way for marketing and user retention. Now if we combine two awesome things here—push notifications backed up with geofence data—we get yet another level of awesomeness. Let’s consider an example: say you have an app for a coffee house that has multiple outlets across the world. A frequent user passes by one of the outlets and the app detects it by using a geofence spread 1km around the outlet location. This geofence entry can be used as a trigger to send a push notification to the user saying something like “We see you’re nearby, how about you take a break and enjoy your favorite cappuccino!”
- Artificial Intelligence: Another very good use-case for geofences in AI is an application like Google Nest thermostats where the intent is to automatically adjust temperature based on user presence. Now how would you detect that a user has entered the house or left it? The Nest app can use the user's home location to create a geofence—the entry and exit events right there for you to perform actions pertaining to the thermostat!
All good things come at a price 🤔
But you may want to think twice if Geofence really is a go-to solution for the problem you are trying to solve, as there are a few overheads that come along with this API:
- Geofencing requires the approval of the user. If the user rejects or disables the location services, geofencing won’t work until enabled from the app settings. So you need to handle this case gracefully in your application.
- Location tracking consumes too much battery life and this is the primary reason why users turn off the location services in their device.
- Geofences are usually best suited when the area you want to cover is more than 100 meters or the battery trade off won’t be worth it.
There are also a few common mistakes that you need to avoid while implementing geofences. Let’s look at them one by one:
- Geofence is too small: Keeping the radius small makes testing a geofence easier, but might not trigger geofence callbacks at all in real-world situations. For best results, the minimum radius of the geofence should be set between 100–150 meters.
- Network is a bit iffy: Geofence service depends on the network location provider which in turn requires a data connection. If there is no reliable data connection, alerts might not be generated.
- Delayed alerts: To optimize for battery life the geofence service is designed in such a way that it doesn’t continuously query for location. The latency is usually less than 2 minutes if the device has been moving but could be in the order of a few minutes if the Background Location Limits are in effect. Moreover, if the device has been stationary for a significant period, the delay could even be as much as 6 minutes.
- Geofence not re-registered: There are some cases when a geofence you create can get lost, requiring that you re-register it again:
- the device is rebooted
- app data is cleared
- Google Play Services data is cleared
- Geofence expired: When creating a geofence, you must also specify the expiration duration for the geofence after which the Location Service automatically removes it. In the event that you don’t receive a trigger once it has expired, you may want to check if that geofence has already expired.
- Your app exceeded the geofence limit: You can have multiple active geofences, with a limit of 100 per app, per device user. In the event that your app has exceeded this limit, you won’t be allowed to create any more geofences 😞
Implementing a geofence 📖
For boilerplate code and implementation you can check Android Developers documentation here.
That’s all for today folks!!! Thanks for giving my first article a read, happy learning! 🙂