Over a million developers have joined DZone.

Geofencing in Windows Phone 8.1

DZone's Guide to

Geofencing in Windows Phone 8.1

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

Windows Phone 8.1 brings the concept of Geofencing to our beloved Windows Phone platform. Geofencing allows us to trigger an action whenever a user is at a predefined set of coordinates. Windows Phone 8.1 registers geofences with the OS so our app doesn’t need to run for triggers to fire. For this post we’ll trigger a welcome message whenever someone is at building 9 of the Microsoft Redmond campus.

The app

Create a new Windows Phone 8.1 app. Add the Location capability in the Package.appxmanifest. We obviously need location capabilities for this Glimlach

Let’s dive into code. This app will only have a blank MainPage and a message dialog that pops up when we’re near building 9.

First thing we need is a reference to the current GeofenceMonitor.

    GeofenceMonitor _monitor = GeofenceMonitor.Current;

Setting the Geofence location and hooking up the event happens in the constructor

    public MainPage()
        _monitor.GeofenceStateChanged += MonitorOnGeofenceStateChanged;
        //Microsoft Redmond building 9
        BasicGeoposition pos = new BasicGeoposition { Latitude = 47.6397, Longitude = -122.1289 };
        Geofence fence = new Geofence("building9", new Geocircle(pos, 100));
        catch (Exception)
            //geofence already added to system

First we hook up the GeofenceStateChanged event, we’ll look at the eventhandler in a minute. Then we create a BasicGeoPosition which holds the coordinates for, in our case, building 9 of the Microsoft campus in Redmond. Then we create a Geofence. The Geofence constructor takes two parameters, a string that functions as ID so it should be unique, and an IGeoShape. Other overloads for Geofence allow us to specify if we want to trigger the event whenever we enter or leave the geofence area.

For the ID we use a string, for the IGeoShape we use a GeoCircle. Other build-in possibilities are

  • Geoboundingbox
  • Geocircle
  • Geopath
  • Geopoint

The Geocircle takes two parameters, the position and the radius (in meters). We’ve already defined a position as BasicGeoposition, which is exactly the type expected by the Geocircle, and we define a radius of 100 meters. Finally we try to add the Geofence to the geofences registered in the OS. If a Geofence with the same ID already exists it will throw an exception stating that the object already exists.

Let’s have a look at the event handler

private void MonitorOnGeofenceStateChanged(GeofenceMonitor sender, object args)
    var fences = sender.ReadReports();
    foreach (var report in fences)
        if (report.Geofence.Id != "building9")
        switch (report.NewState)
            case GeofenceState.Entered:
                Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
                    MessageDialog dialog = new MessageDialog("Welcome to building 9");
                    await dialog.ShowAsync();
            case GeofenceState.Exited:
                Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>

Before we can show a message we’ll need to know what geofence triggered the event. We do that by calling ReadReports on the GeofenceMonitor. This method will return a ReadOnlyList containing GeofenceStateChangedReports. Iterating over those reports we can check the ID of the geofence and take action when the expected geofence is triggered. In this case we want to show a welcome message when someone arrives and a leaving message when someone leaves. Let’s try it out!

Launch the app in the emulator (unless you’re physically at building 9, then you can use a WP8.1 device Glimlach), open the emulator tools and set the location to building 9. You’ll have to wait some seconds before the message pops up, this is to prevent triggers being fired when just passing through a geofence location. You can manipulate this delay by using an overload of the Geofence constructor and setting the DwellTime parameter (TimeSpan).

Clear your location and set it somewhere away from building 9, wait a few seconds and watch the leaving message.


Geofencing is a pretty cool way to interact with users based on their location. It’s a great addition to the platform and personally I can’t wait to see what great ideas devs will come up with to use this functionality.

In this post I did a quick, basic lap around geofencing. Geofences can also trigger a backgroundtask, provided the app is set as lockscreen app, so the app doesn’t need to run all the time for geofencing to do its stuff.

As usual, the code can be found on my OneDrive

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}