Respond to Location Updates in Windows Store Apps


Today we explain how to respond to location updates from location changes in Windows Store apps using C# and XAML. The example in this article demonstrates how to use the Geolocation API to get the geographic location of the user's PC. An app can use the Geolocation API to get the location once or it can continuously track the location. This example demonstrates both ways of using the Geolocation API.

In this example we are using the two namespaces "Windows.Devices.Geolocation" and "Windows.UI.Core" for tracking user location information. When you run the app the first time, you'll get a prompt that asks if the app can use your location. Choose the Allow option. Click the Get Location button to get the current location.

Step 1

Open Visual Studio 2012 and start a new Windows Store apps project.

Step 2

Go to the Solution Explorer and open "Package.appxmanifest".


Step 3

In "Package.appxmanifest" go to the "Capability" tab and enable the "Location" option.


Step 4

Go to the "MainPage.xaml" page and replace all code with the following code.










    <Grid x:Name="LayoutRoot" Background="SteelBlue">

        <Button Content="Tracking Location"  x:Name="TracLoc"

         HorizontalAlignment="Left" Margin="12,180,0,0"

         VerticalAlignment="Top" Width="151" Click="TracLoc_Click" />

        <Button Content="Stop Tracking"

         x:Name="StopTrac" HorizontalAlignment="Left" Margin="212,180,0,0"

         VerticalAlignment="Top" Width="151" Click="StopTrac_Click" />


        <TextBlock Margin="14,230,0,0" FontSize="18" Text="Latitude" Name="textLatitude" />

        <TextBlock Margin="14,270,0,0" FontSize="18" Text="Longitude" Name="textLongitude" />

        <TextBlock Margin="14,310,0,0" FontSize="18" Text="Accuracy" Name="textAccuracy" />




Step 5

Your "MainPage.xaml.cs" is as the following code.

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;

using Windows.Devices.Geolocation;

using Windows.UI.Core;


namespace ResponceLocationUpdate


    public sealed partial class MainPage : Page


        private Geolocator Geo = null;

        private CoreDispatcher CoreDis; 

        public MainPage()



            CoreDis = Window.Current.CoreWindow.Dispatcher;


        protected override void OnNavigatedTo(NavigationEventArgs e)


            TracLoc.IsEnabled = true;

            StopTrac.IsEnabled = false;


        private void TracLoc_Click(object sender, RoutedEventArgs e)


            if (Geo == null)


                Geo = new Geolocator();


            if (Geo != null)


                Geo.PositionChanged +=

                 new TypedEventHandler<Geolocator,



            TracLoc.IsEnabled = false;

            StopTrac.IsEnabled = true;



        private void StopTrac_Click(object sender, RoutedEventArgs e)


            if (Geo != null)


                Geo.PositionChanged -= new TypedEventHandler<Geolocator, PositionChangedEventArgs>(LocationChanged);

                textLatitude.Text = "Latitude: ";

                textLongitude.Text = "Longitude: ";

                textAccuracy.Text = "Accuracy: ";

                TracLoc.IsEnabled = true;

                StopTrac.IsEnabled = false;



        async private void LocationChanged(Geolocator sender, PositionChangedEventArgs e)


            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>


                Geoposition pos = e.Position;

                textLatitude.Text = "Latitude: " + pos.Coordinate.Latitude.ToString();

                textLongitude.Text = "Longitude: " + pos.Coordinate.Longitude.ToString();

                textAccuracy.Text = "Accuracy: " + pos.Coordinate.Accuracy.ToString();





Step 6

Run the program and click "Tracking Location". The first time it will ask to allow tracking. Click on "Allow".


Step 7

Your location information is as the following.


Similar Articles