EDN Admin
Well-known member
In this weeks Inside Windows Phone we talk about Location and Mapping in Windows Phone 8.
In this video we cover
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()
{
InitializeComponent();
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)
{
try
{
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 dont 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, lets also set the zoom level to 14.
async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
try
{
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, lets 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, well 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(
args.Position.Coordinate.Latitude,
args.Position.Coordinate.Longitude);
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. 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 dont 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
View the full article
In this video we cover
- The basics of location and mapping
- 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.
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()
{
InitializeComponent();
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)
{
try
{
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 dont 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, lets also set the zoom level to 14.
async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
try
{
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, lets 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, well 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(
args.Position.Coordinate.Latitude,
args.Position.Coordinate.Longitude);
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. 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 dont 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
View the full article