Random geekery since 2005.

Husband, father, programmer, speaker, writer, blogger, podcaster, collector, traveler, golfer.

This post is Day #13 in a series called the 31 Days of Windows Phone.

Yesterday, we talked about the VibrateController on Windows Phone, and how we notify our users that they need to pay attention to what’s happening on their device.  Today, we’re going to talk about the LocationServices, and how we can get information about where their device is.

Why is location important?

As devices go, mobile phones have the ability to give you more customized information than nearly any other.  This is specifically relevant when we talk about the location of the phone.  Here’s some specific examples (that I’ve made up for this article).

Field Documentation App

Let’s pretend that I have an application that contains (or accesses via web) thousands of schematics, part lists, and documentation for all of the products and devices that we have built and service for our customers.  On most every device, I have to search for the customer I’m working with today, find the specific office or site I’ll be visiting, and then parse through the dozens of documents that might still meet my criteria.

By adding location data to this application, we remove the need for the user to use search at all.  If each document was also tagged with geolocation data, as we approach the building, the specific documents for that site could imediately become available.  If we can get a specific enough location on a large facility, we might even be able to determine which device you’re near.  Using a device’s location to determine the appropriate data to show is one way of enhancing your application with location data.

Golfing Application

I’ve seen many golfing applications that allow you to enter your scores, track your handicap, and even share that data with your friends.  There’s one, specifically, however that stands out in my mind.

This application, when it starts up, recognizes which golf course you are currently at.  It also lists ones that are nearby (in case you are en route), but not having to look up the course is an awesome feature.  In addition, it recognizes which hole you’re on, and what the distance to major landmarks on the course are (sandtraps, water, tree, green, etc.).  One of the coolest features I’ve seen, though, is the shot-by-shot analysis.  Each time I hit a club, I tell it which one it was.  As I head to where the ball is, it actually keeps track of how far I hit each club, and with enough data, can even recommend a club to me based on my distance to the green.  Absolutely amazing uses of location data.

Understanding Location Services

There are three ways we can get data about the device’s location.  GPS, cell-phone triangulation, and Wi-fi location.  Here’s a quick glance at the strengths and weaknesses of each:


Before you start worrying about having to compare and contrast the data from three independent sources, take a look at the green cloud in the picture above.  Microsoft has provided us with a set of Location services.

Using Location Services on Windows Phone 7

I’ve written plenty so far, but nothing about the code required to make this happen.  Here’s the good news:  this might be one of the easiest things you do in code.  Just follow these steps:

  1. Create a new GeoCoordinateWatcher object.
  2. Create an event handler for when the user’s position changes.
  3. Grab the data when that event fires.

You’ll likely want to offer your user more functionality than this (and my sample code below does just that), but for the plain-and-simple example, here’s the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Device.Location;
using Microsoft.Phone.Tasks;

namespace Day13_LocationServices
	public partial class MainPage : PhoneApplicationPage
		GeoCoordinateWatcher gcw;
		// Constructor
		public MainPage()
			gcw.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(gcw_PositionChanged);

		void gcw_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
			Latitude.Text = e.Position.Location.Latitude.ToString();
			Longitude.Text = e.Position.Location.Longitude.ToString();


Like the accelerometer on Day #11, the emulator does not actually have a way to get location data.  If you run the above code in the emulator, you’ll get no data.  (You can check GoeCoordinateWatcher.Status for this info).  However, using our good friend Reactive Extensions, we can emulate this data as well.

There is an outstanding article on MSDN about how to implement this, so I won’t try and trump their efforts here.  You can check out the MSDN article about Reactive Extensions and Location on Windows Phone here.

Download the Code

In my sample application, I tried to take advantage of as much of the Location services as I could.  I let the user choose between High and Default accuracies, start and stop the service, and even create a map of the current coordinates in the application.  Check it out.  Far more robust than the example above.



4 responses to “31 Days of Windows Phone | Day #13: Location Services”

  1. […] Phone Emulators ist für Standortdaten. Um Positionsdaten zu beobachten, können Sie den Code von Day #31 der 31 Days of Windows Phone […]

  2. […] La segunda tabla en el emulador de Windows Phone es la de localización. Para monitorear la información de tu ubicación , puedes utillizar el código del Día 13 de 31 días con Windows Phone […]

  3. […] Location Services, http://www.jeffblankenburg.com/2010/10/13/31-days-of-windows-phone-day-13-location-services/ (note: this post was written for the original WP7 release–the GPS works the same, but the […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

%d bloggers like this: