Tracking Location in Windows Phone 8.1

Windows Phone 8.1 comes with many prospects for geo location and usually the next thing after detecting your location, people tend to look for something else and that is tracking the location since it should update itself. So, if you're wondering how to instantiate a map and track your location, you really should check out my previous article.

And if you have already done that then I hope you liked it. Then you probably already understand how to do the following:

  1. Please proceed to your Visual Studio (I'm using Visual Studio 2013 Community Edition) and create a new Blank App project on Windows Phone 8.1.

  2. Go over your project properties and go over Capabilities. Enable Location for your app. (If you are from Windows Phone 8, you will be pretty amazed that you don't see ID_CAP_MAP anymore.)

A Basic UI

Let's set up a basic UI and yes the following is kind of a rip-off from MSDN but as long as it helps people, it's cool. Our little UI has two Buttons and a number of textblocks and looks as in the following in XAML (I'm only posting the main grid).

If that seems a bit too much XAML for you then calm down because It looks extremely basic too.

Now, let's move towards the MainPage.xaml.cs part. As the previous article suggests, the first thing you need is definitely a Geolocator. So please proceed and define a Geolocator instance.
  1. private Geolocator locator = null
Now that we have a geo locator we can move ahead and go over the Properties of TrackLocationButton and create the method stub for a Click event. And the event handler looks as in the following:
  1. private void TrackLocationButton_Click(object sender, RoutedEventArgs e)    
  2. {    
  3.     if (locator == null)    
  4.     {    
  5.         locator = new Geolocator();    
  6.     }    
  7.     if (locator != null)    
  8.     {    
  9.         locator.MovementThreshold = 3;    
  11.         locator.PositionChanged +=    
  12.             new TypedEventHandler<Geolocator,    
  13.                 PositionChangedEventArgs>(locator_PositionChanged);    
  16.     }    
  18.     TrackLocationButton.IsEnabled = false;    
  19.     StoptrackingButton.IsEnabled = true;    
  21. }  
Now, if you dive a little bit into that, you will see that if the locator is instantiated then the first property we change is the MovementThreshold property. It is the property that defines the movement threshold for the geo tracking to notify you. It's defined in meters and thus we have defined it to 3 meters. Now the next question is how would I know the distance that I have moved my MovementThreshold. The common answer would be to actually invoke a PositionChanged event and attach a handler to it. And the handler locator_PositionChanged looks as in the following:
  1. async private void locator_PositionChanged(Geolocator sender, PositionChangedEventArgs e)    
  2. {    
  3.     await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>    
  4.     {    
  5.         Geoposition geoPosition = e.Position;    
  6.         LatitudeText.Text = geoPosition.Coordinate.Point.Position.Latitude.ToString();    
  7.         LongitudeText.Text = geoPosition.Coordinate.Point.Position.Longitude.ToString();    
  8.         AccuracyText.Text = geoPosition.Coordinate.Accuracy.ToString();    
  9.     });    
  10. }  
Now even before describing whats being done here you might wonder where the dispatcher comes from, the truth is it has been declared before as:
  1. private CoreDispatcher dispatcher;  
  3. public MainPage()  
  4. {  
  5.     this.InitializeComponent();  
  6.     dispatcher = Window.Current.CoreWindow.Dispatcher;  

The reason we are using that will be pretty clear if we look at what are we doing. We are constantly updating our LatitudeText, LongitudeText, AccuracyText and since we are accessing a UI thread frequently from an event handler, we do need to make ensure it doesn't block the regular UI interactions. Thus we used a CoreDispatcher here, much like it was in Windows Phone 8 as Dispatcher. You'd also be able to notice that the event handler is async and thus dispatcher.RunAsync invocation was awaited. We used CoreDispatcherPriority.Normal and you can even try a higher priority if you like. All this event handler is doing is actually updating the UI and the textblocks.

Now all you need is a StoptrackingButton to get things to stop and all you need to do is unhook the locator_PositionChanged event handler.

  1. private void StoptrackingButton_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     if (locator != null)  
  4.     {  
  5.         locator.PositionChanged -= new TypedEventHandler<Geolocator, PositionChangedEventArgs>(locator_PositionChanged);  
  6.     }  
  8.     StoptrackingButton.IsEnabled = false;  
  9.     TrackLocationButton.IsEnabled = true;  

Debugging it in the emulator

Since this sample has geolocator usage, this is always better to be tested in a device but you can definitely try it on an emulator too. To test it load the app on the emulator first by invoking build.

When the emulator hooks up open up the emulator tools and go to the location tab and click on the map to provide you a primary location first. You can have a look at the GIF below for an idea:

Now you have pointed to a primary location in the map. Now you can try clicking the track location button. After you do that the Latitude, Longitude and Accuracy will pop up for the place you clicked on the map. Now click some more around it. And you will see the data changes on the textblocks. You can do one thing more too. You can change the interaction mode from Live to Pin and play all the pins. That would even allow you to emulate place changes in your emulator. The following GIF would definitely help you.
I hope that helps any of the newbies out. Stay Frosty! 

Similar Articles