Loading user information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading user information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

IWP 57 | Location and Mapping for Windows Phone 8

25 minutes, 52 seconds


Right click “Save as…”

In this week's Inside Windows Phone we talk about Location and Mapping in Windows Phone 8.

In this video we cover

  1. The basics of location and mapping
  2. The power of Windows Phone location and mapping APIs including simple ways to do distance calculations, reverse geocoding (get an address from a latitude/longitude), mapping routes (both walking and driving) and how to point users to download offline maps to drive a powerful mapping experience available worldwide even in the absence of network connectivity.

All code from this video is available here.

Getting started with location in Windows Phone takes about 5 minutes.

Windows Phone Location and Mapping in 5 Minutes

Minute 1: Enable your app for mapping and location tracking.

Open up a new Windows Phone 8 project. Double-click on the WMAppManifest.xml file


Go to the Capabilities tab and check the ID_CAP_LOCATION and ID_CAP_MAP capabilities.


This gives you application permission to use the location APIs and the Map control.

Minute 2:  Add a map control.

Open up your MainPage.xaml in either Visual Studio or Blend. Go to the Toolbox in Visual Studio or the Assets pane in Blend and type "Map" in the search box. Double click or click-and-drag to add it to your XAML control.

In Visual Studio 2012:

In Blend for VS 2012

Minute 3: Get your location

Open up MainPage.xaml.cs (also known as your code-behind). Add a Geolocator variable and initialize it in your constructor. You will need to add Windows.Devices.Geolocation as a reference.

Geolocator locator;  

public MainPage()
    locator = new Geolocator();
    this.Loaded += MainPage_Loaded;

Change your MainPage_Loaded to enable await operations by adding "async" before "void" and add the following code:

async void MainPage_Loaded(object sender, RoutedEventArgs e)
        Geoposition myLocation = await locator.GetGeopositionAsync();
        GeoCoordinate geoCord = new GeoCoordinate(myLocation.Coordinate.Latitude, myLocation.Coordinate.Longitude);
    catch (Exception exception)

We want to wrap our attempt to get the Geoposition in a try-catch so that if it fails we don't crash our app. This could fail if the user turns off the ability of the app to get a location or if no location is available due to lack of connectivity and GPS access.

Minute 4: Update the map to your location

Give your map control a name so that we can reference it in our code-behind

<maps:Map x:Name="MyFirstMap" Grid.Row="1"/>

And set the GeoCoordinate that we created in minute 3 to the Map.Center property. To help us orient a little better, let's also set the zoom level to 14.

async void MainPage_Loaded(object sender, RoutedEventArgs e)
        Geoposition myLocation = await locator.GetGeopositionAsync();
        GeoCoordinate geoCord = new GeoCoordinate(myLocation.Coordinate.Latitude, myLocation.Coordinate.Longitude);
        MyFirstMap.Center = geoCord;
        MyFirstMap.ZoomLevel = 16;

// And so on...

Minute 5: Update your location as it changes

Finally, let's set the movement threshold on our Geolocator to 50 meters (the locator object will only raise a position changed event when the device moves more than 50 meters from the last location) and start listening for when the position changes.

locator.DesiredAccuracy = PositionAccuracy.High;
locator.MovementThreshold = 50;
locator.PositionChanged += locator_PositionChanged;
Geoposition myLocation = await locator.GetGeopositionAsync();
// etc...

In the event handler, we'll look at our new position and re-center our map accordingly. One thing we need to remember is that, for performance reasons, this event handler reports on a background thread. In order to update our Map control UI, we need to move to the UI thread using Dispatcher.BeginInvoke.

void locator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
    Dispatcher.BeginInvoke(() =>
        GeoCoordinate newGeoCoord = new GeoCoordinate(
        MyFirstMap.Center = newGeoCoord;

Bonus Minute: Emulate location updates

And now we have an app that tracks our location and displays it on a map. To update the location and watch those updates track in your app, open the additional tools using the button indicated below.

Under the location tab, just click or tap on the map and the updated location will be indicated in your app. You can even load up a file of pre-defined GeoCoordinates (up to 99) and play them in the emulator so you can test constantly changing coordinates in your app. The file we used is a set of coordinates emulating a jog around the Microsoft campus.

You can download that file here.  Load the file by clicking on the Load button under "Recorded Data" in the Location emulator tab. For the best fake running experience have the emulator fire a new point every 5 seconds and hit the play button. For easy access, place the file in the "...My Documents/WindowsPhoneEmulator/Location" folder.

Other Things That Are Easy

The second demonstration app we showed the following features from the Windows Phone API. We don't want to overload this post with too much, so these are available as individual posts.

Calculating Distance Between 2 GeoCoordinates

Reverse Geocoding

Mapping Walking and Driving Routes

Offline Mapping


Follow the discussion

  • Oops, something didn't work.

    Getting subscription
    Subscribe to this conversation
  • I love this show ! Very helpful and inspiring. I have questions:
    Last step that You've made... Getting the token for MapServices is for free ? And there is no additional costs connected with developing and submitting apps with maps to the store ? This map app will work on every phone or only Nokias ? And the last thing... This is available for Windows 8 (tablets, pc's etc.) too ?

  • @dzimiq: Thanks! The token for the Map control is free now and forever. It is just a way to verify that the map control has been signed in the app store.

    Microsoft and Nokia have teamed up to deliver this mapping data to Windows Phone 8 across all devices. This will work on HTC or Samsung or Huawei phones as well as Nokia phones.

    At this time, the Windows 8 solution is to use the Bing Maps control. Although it is being driven by much of the same data, it is a distinct control.

  • Thank You Matthias ! I can't wait for new episodes. I don't have enough free time to use all concepts that are presented in IWP but for sure I will use some of them in my apps.

  • Love the Episode.

    Shows me that what I have in mind is possible with minimal friction.

    I can't help feeling that Larry was a little too rough on Mathias with the constant direction in presentation and coaxing.

    Other than that, IWP is my new favorite channel9 show.

  • A tad bit confused, if MainPage_Loaded is void returning and async, when the app calls the first await will now return and leave MainPage_Loaded? Sometime later locator.GetpositionAsync will return something and cause and exception. Most people I have seen using await in a  void returning event handler do not return a task. I am confused, because this goes against what Luican W. said on his Channel 9 series on Async. Will the statement after The await be executed? Will the catch (not shown) catch the exception?

    async void MainPage_Loaded(object sender, RoutedEventArgs e)
            Geoposition myLocation = await locator.GetGeopositionAsync();
            GeoCoordinate geoCord = new GeoCoordinate(myLocation.Coordinate.Latitude, myLocation.Coordinate.Longitude);
            MyFirstMap.Center = geoCord;
            MyFirstMap.ZoomLevel = 16;


Remove this comment

Remove this thread


Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.