Geo-location on Windows 8* desktop applications using WinRT

Download Article

Geo-location on Windows 8* desktop applications using WinRT (PDF 342KB)

One of the main differentiations found on the new Ultrabook™ devices is the inclusion of sensors. On Intel® Developer Zone, you can find many articles about how to write apps that use those sensors to deliver exciting and creative Ultrabook user experience. In this article, we will talk about another high-demand sensor, GPS, and how to make your application aware of geolocation features. We will be using a sample project in XAML and C# for this example, created on Visual Studio* 2012.


Before jumping in and writing code, it’s important for developers to consider and respect user privacy. In the new Windows 8 UI, when your application tries to find the system localization, users will be prompted whether or not they want this to happen. This is a security feature. Using our desktop example depends on the user setting allowing applications to use location or not. Users may not allow an application to access their location, so an exception needs to be written for this case.

Enabling GPS on desktop applications

The Windows Runtime (WinRT) provides support for sensors for applications designed for the new Windows 8 UI. Some of those modules are also exposed to desktop applications, and geolocation is one of those modules. However, we need some special setups to access this module, and this is very well covered in this article. Please make sure you include the required references as explained in the link. If you need help creating a new application on Visual Studio, you can find information on this other webpage.

Once you have the references added, you should be able to add the Windows.Devices.Geolocation module into your code definitions.

using Windows.Devices.Geolocation;

To trigger geolocation, let’s place a button on our MainWindow.xaml file and declare that a click would start our location function. Add the following code inside the <Grid> declaration:

<Button Name="GetGeoLocation" Background="Transparent" Height="50" Click="GetGeoLocation_Click">Get Location</Button>

If you right-click on “GetGeoLocation_Click” and select View Code, you will be redirected to the C# file defining the function.

The class we will use is Geolocator and Geoposition. The Geolocator provides several functionalities, including location and tracking position changes. The Geoposition holds the position in coordinates or civic address. Unfortunately, at this time the civic address hasn’t been mapped, so you will need to use a maps provider to resolve the address with the coordinates provided.

So include this code into the GetGeoLocation_Click function:

Geolocator gl = new Geolocator();
Geoposition gp = await gl.GetGeopositionAsync();

It seems very simple, but there is a trick. The function responsible for returning the position is asynchronous and needs to be treated by the await declaration. This means the code will return to the point before calling the GetGeoLocation_Click function and wait for a signal indicating the GetPositionAsync function returned a value. To enable that, we need to change our GetGeoLocationClick function returning type to async. The new code would be like this:

async void GetGeoLocation_Click_1(object sender, RoutedEventArgs e)
            Geolocator gl = new Geolocator();
            Geoposition gp = await gl.GetGeopositionAsync();

Visual Studio may complain about await. You need to add another reference to your project for Windows compatibility libraries, System.Runtime.WindowsRuntime.dll and System.Threading.Tasks.dll. This should fix the problem.

The Geoposition structure will have the position returned by the device in latitude and longitude. If you want to check it, you can show it in a dialog or print it on the console:

Console.WriteLine("Latitute: " + gp.Coordinate.Latitude.ToString() + 
", Longitute: " + gp.Coordinate.Longitude.ToString());

The basics for using Geolocation are very simple because the information required is very simple. What to do with that information then is the open playground for developers to get creative and find usages for people who want to use it. Hope you get inspired and leave a comment on what you do with it!


Intel, the Intel logo, and Ultrabook are trademarks of Intel Corporation in the US and/or other countries.

Copyright © 2012 Intel Corporation. All rights reserved.

*Other names and brands may be claimed as the property of others.

Per informazioni più dettagliate sulle ottimizzazioni basate su compilatore, vedere il nostro Avviso sull'ottimizzazione.