In this posting, we’ll look at how to track location coordinates in Windows 8. (Click on image for full size)
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,
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition Height="6*" />
</Grid.RowDefinitions>
All of the TextBlocks will use the Style “Output” .
<Page.Resources>
<Style x:Name="Output"
TargetType="TextBlock">
<Setter Property="FontFamily"
Value="Segoe UI" />
<Setter Property="FontSize"
Value="20" />
<Setter Property="Margin"
Value="10" />
</Style>
</Page.Resources>
The labels will identify the values, and the TextBlocks in column 1 (the second column) will display their values,
GeoLocation Data
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,
public sealed partial class MainPage : Page
{
private Geolocator location;
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,
protected override void OnNavigatedTo( NavigationEventArgs e )
{
location = new Geolocator();
location.PositionChanged += location_PositionChanged;
}
protected override void OnNavigatedFrom( NavigationEventArgs e )
{
base.OnNavigatedFrom( e );
location.PositionChanged -= location_PositionChanged;
}
Location Changed
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,
async void location_PositionChanged( Geolocator sender, PositionChangedEventArgs args )
{
await Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () =>
{
Geoposition position = args.Position;
LatitudeTB.Text = position.Coordinate.Latitude.ToString();
LongitudeTB.Text = position.Coordinate.Longitude.ToString();
AccuracyTB.Text = position.Coordinate.Accuracy.ToString() + " meter(s)";
TimestampTB.Text = position.Coordinate.Timestamp.ToString();
AltitudeTB.Text = position.Coordinate.Altitude.ToString() + " meter(s)";
} );
}
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.
Great tutorial, thank you 🙂
How would I update the position more often? I know I can do it with Geolocator MovementThreshold or ReportInterval properties but how to use properties in the above code? For example setting ReportInterval to 2 seconds.