In order to get this to work, we need to be able to obtain the values we’re displaying (e.g., for Latitude and Longitude) . Let’s start by looking at how we can get the location values.
To get set up, we’ll create a new Windows 8 Blank application in C# and XAML. Double click on the Package.appxmanifest and click on the Capabilities tab. There we’ll want to check the Location box to allow use of location services. The user still must give permission when the application runs, but this is taken care of for you by the operating system.
We begin in MainPage.xaml by setting up textblocks; some to act as labels and others to hold the values we’ll retrieve from the location services. To do this, add six RowDefinitions to your grid,
All of the TextBlocks will use the Style “Output” .
The labels will identify the values, and the TextBlocks in column 1 (the second column) will display their values,
Let’s see how we gather the geolocation data and attach the values to the TextBlocks. In MainPage.xaml.cs we start by defining an object of type Geolocator,
As noted in the Microsoft help page this class supports two events:
- PositionChanged – raised when the location updates
- StatusChanged – raised when the ability of the Geolocator to provide updated locations changes
We will create handlers for the first of these, registering the handlers in when we navigate to the page, and de-registering when we leave the page,
Each time the location changes the PositionChanged event is raised and our handler method is called. In that event handler, we will asynchronously obtain the data we need to update the position TextBlocks,
At this point you can make use of the data in a variety of ways. In this blog post I go on to plot the data using Telerik controls.