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:
- 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.
- 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.
- 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:
- private void TrackLocationButton_Click(object sender, RoutedEventArgs e)
- {
- if (locator == null)
- {
- locator = new Geolocator();
- }
- if (locator != null)
- {
- locator.MovementThreshold = 3;
-
- locator.PositionChanged +=
- new TypedEventHandler<Geolocator,
- PositionChangedEventArgs>(locator_PositionChanged);
-
-
- }
-
- TrackLocationButton.IsEnabled = false;
- StoptrackingButton.IsEnabled = true;
-
- }
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:
- async private void locator_PositionChanged(Geolocator sender, PositionChangedEventArgs e)
- {
- await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
- {
- Geoposition geoPosition = e.Position;
- LatitudeText.Text = geoPosition.Coordinate.Point.Position.Latitude.ToString();
- LongitudeText.Text = geoPosition.Coordinate.Point.Position.Longitude.ToString();
- AccuracyText.Text = geoPosition.Coordinate.Accuracy.ToString();
- });
- }
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:
- private CoreDispatcher dispatcher;
-
- public MainPage()
- {
- this.InitializeComponent();
- 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.
- private void StoptrackingButton_Click(object sender, RoutedEventArgs e)
- {
- if (locator != null)
- {
- locator.PositionChanged -= new TypedEventHandler<Geolocator, PositionChangedEventArgs>(locator_PositionChanged);
- }
-
- StoptrackingButton.IsEnabled = false;
- 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!