geo-fencing image

A geo-fence is a virtual perimeter that maps over some geographic point or object in the real world. These digitally generated spaces can be used in conjunction with your location aware device to produce alerts and behaviors as follows:

  • Request to be notified when the device enters a geo-fence.
  • Request to be notified when the device leaves a geo-fence.
  • Specify a time window during which the geo-fence is active.
  • Specify a dwell time for each geo-fence (amount of time that the device should be in or out of an area).

The use-cases even for this partial list is quite large in both a professional and personal context, however, one of the best applications I heard of was at the Kenya Wild Life Service. “Near threatened” bull elephants were constantly leaving a conservation park and terrorizing local farmers, one particular elephant (Kimani) had a particularly long history of raiding crops…

The race to save Kimani began two years ago. The Kenya Wildlife Service had already reluctantly shot five elephants from the conservancy who refused to stop crop-raiding, and Kimani was the last of the regular raiders. The Save the Elephants group wanted to see if he could break the habit.

So they placed a mobile phone SIM card in Kimani's collar, then set up a virtual "geofence" using a global positioning system that mirrored the conservatory's boundaries. Whenever Kimani approaches the virtual fence, his collar texts rangers.

They have intercepted Kimani 15 times since the project began. Once almost a nightly raider, he last went near a farmer's field four months ago.

This topic applies to many location aware devices, but I will be covering the shared API that applies to Windows hardware (phones, tablets and laptops).

Windows.Devices.Geolocation.Geofencing Namespace

The Geofence class is the main construct for defining a geo-fence, it contains data points that define its shape and duration. The Geocircle is currently the only shape supported but, it is implemented as an interface which could support any shape you saw fit to create. The Geofence class is in turn supported by GeofenceMonitor, its primary task is to manage and assess the state of your defined geo-fences, and the following shows that in action.

private void CreateGeofence(double lat, double lng, double rad)
{
var position = new BasicGeoposition
{
Latitude = lat,
Longitude = lng
};

var circle = new Geocircle(position, rad);
var mask = MonitoredGeofenceStates.Entered | MonitoredGeofenceStates.Exited;
var dwellTime = TimeSpan.FromHours(24);

var geofence = new Geofence(Guid.NewGuid().ToString(), circle, mask, false, dwellTime);

GeofenceMonitor.Current.Geofences.Add(geofence);
}

Foreground Geofencing Detection

Once a geo-fence is created and stored we need a mechanism to respond to a breach in its boundaries, in the following code sample we accomplish this task in the foreground directly within the page of an app:

using Windows.UI.Core;
using Windows.UI.Popups;
using Windows.Devices.Geolocation.Geofencing;


namespace PhoneApp1
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
Windows.Devices.Geolocation.Geofencing.GeofenceMonitor.Current.GeofenceStateChanged
+= Current_GeofenceStateChanged;

}

async void Current_GeofenceStateChanged(Windows.Devices.Geolocation.Geofencing.GeofenceMonitor sender,
object args)
{
var reports = sender.ReadReports();

await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
foreach (GeofenceStateChangeReport report in reports)
{
var state = report.NewState;
var geofence = report.Geofence;

if (state == GeofenceState.Entered)
{
var dialog = new MessageDialog(String.Format("Entered GeoFence {0}, your dwell time is {1}",
geofence.Id, geofence.DwellTime), "Entering A GeoFence");
await dialog.ShowAsync();
}
else if (state == GeofenceState.Exited)
{
var dialog = new MessageDialog(String.Format("Exited GeoFence {0}, your dwell time is {1}",
geofence.Id, geofence.DwellTime), "Exit A GeoFence");
await dialog.ShowAsync();
}
}
});

}

}
}

Security & Privacy

When developing apps that use geo-fencing technologies developers have a fiduciary responsibility to treat this data with extreme caution and care. A user's geographic location is personally identifiable information (PII). At a minimum you should do the following:

  • Get the users consent.
  • Tell the user how location data will be used.
  • Clear all cached location data when the user disables access to location info.


Related Posts