Nico's digital footprint

I grew up in the nineties, that makes me awesome by default

LowlandsWPDev 2014 slides & demos

by Nico

Last weekend I had the distinct honor and pleasure of co-organizing and speaking at an international Windows Platform developer event. Lowlands WP dev 2014 was a joint venture between the Dutch and the Belgian WP communities and it was awesome!

The slides and demos for my session on Background tasks can be found here:

Demos

 
PIctures of the event itself can be found here

Tags:

.Net | Community | Presenting | Talk | XAML | geofencing

Geofencing in Windows Phone 8.1

by Nico

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.

Code Snippet
  1. GeofenceMonitor _monitor = GeofenceMonitor.Current;

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

Code Snippet
  1. public MainPage()
  2. {
  3.     InitializeComponent();
  4.  
  5.     _monitor.GeofenceStateChanged += MonitorOnGeofenceStateChanged;
  6.  
  7.     //Microsoft Redmond building 9
  8.     BasicGeoposition pos = new BasicGeoposition { Latitude = 47.6397, Longitude = -122.1289 };
  9.  
  10.     Geofence fence = new Geofence("building9", new Geocircle(pos, 100));
  11.  
  12.     try
  13.     {
  14.         _monitor.Geofences.Add(fence);
  15.     }
  16.     catch (Exception)
  17.     {
  18.         //geofence already added to system
  19.     }
  20. }

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

Code Snippet
  1. private void MonitorOnGeofenceStateChanged(GeofenceMonitor sender, object args)
  2. {
  3.     var fences = sender.ReadReports();
  4.  
  5.     foreach (var report in fences)
  6.     {
  7.         if (report.Geofence.Id != "building9")
  8.             continue;
  9.  
  10.         switch (report.NewState)
  11.         {
  12.             case GeofenceState.Entered:
  13.                 Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
  14.                 {
  15.                     MessageDialog dialog = new MessageDialog("Welcome to building 9");
  16.  
  17.                     await dialog.ShowAsync();
  18.                 });
  19.                 break;
  20.             case GeofenceState.Exited:
  21.                 Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
  22.                 {
  23.                     MessageDialog dialog = new MessageDialog("Leaving building 9");
  24.  
  25.                     await dialog.ShowAsync();
  26.                 });
  27.                 break;
  28.         }
  29.     }
  30. }

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.

Conclusion

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


Tags:

WinRT | WP8 | geofencing

  Log in

About the author

Hi,

My name is Nico, I’m an MVP Windows Platform Development living in Belgium.
I’m currently employed as a .NET consultant at RealDolmen, one of Belgium’s leading IT single source providers.

I'm also founding member and board member of the Belgian Metro App Developer Network, a user group focussed on Windows 8 and Windows Phone development. If you're in Belgium feel free to drop by if we're doing an event. http://www.madn.be

Since June 2012 I'm a proud member of Microsoft's Extended Experts Team Belgium. And in February 2013 I became a member of DZone's Most Valuable Bloggers family.

In 2013 I became a book author and wrote "Windows 8 app projects, XAML & C# edition".

In 2014 I received the MVP award for the very first time.

I hope to get feedback from my readers either through comments, mail (nico_vermeir@hotmail.com), twitter, facebook, …

 

mvp

 

mvp

 

 

MeetLogo

 

MVBLogo

mybook

 

Month List