[This topic is featured in Develop great apps for Windows 8.]
This topic shows you how to detect geographic location.
Roadmap: How does this topic relate to others? See:
Objective: After you complete this quickstart, you will understand how to create a simple app that gets the user's current geographic location.
Prerequisites
You should be familiar with XAML, Visual C#, and events.
Access to location must be enabled on your device.
Time to complete: 20 minutes.
Instructions
1. Open Microsoft Visual Studio 2012
Open Microsoft Visual Studio 2012.
2. Create a new project
Create a new project, choosing an Application from the Visual C# > Windows Store project types.
3. Insert the application C#
Open your project's MainPage.xaml.cs file and replace the existing code with the following.
using System; using System.Collection.Generic; using System.Linq; using System.Threading.Tasks; using Windows.Foundation; using Windows.UI.DirectUI; using Windows.UI.DirectUI.Controls; using Windows.UI.DirectUI.Data; using Windows.Devices.Geolocation; namespace GeolocationSample { partial class MainPage { Geolocator geo = null; public MainPage() { InitializeComponent(); } private async void button1_Click( object sender, RoutedEventArgs e) { if (geo == null) { geo = new Geolocator(); } IGeoposition pos = await geo.GetGeopositionAsync(); textblockLatitude.Text = "Latitude: " + pos.Coordinate.Latitude.ToString(); textblockLongitude.Text = "Longitude: " + pos.Coordinate.Longitude.ToString(); textblockAccuracy.Text = "Accuracy: " + pos.Coordinate.Accuracy.ToString(); } } }
You'll need to replace the class name in the previous snippet with the name of your application's class. For example, if you created a project named "Application1", you'd replace:
namespace GeolocationSample
with
namespace Application1
4. Insert the application XAML
Open the file MainPage.xaml and copy the following XML into this file (replacing its original contents).
<UserControl x:Class="GeolocationSample.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="1366"> <Grid x:Name="LayoutRoot" Background="Gray"> <Button Content="Get Location Async" Click="button1_Click" Height="23" HorizontalAlignment="Left" Margin="12,180,0,0" Name="button3" VerticalAlignment="Top" Width="120" /> <TextBlock Margin="14,220,0,0" Text="Latitude" Name="textLatitude" /> <TextBlock Margin="14,260,0,0" Text="Longitude" Name="textLongitude" /> <TextBlock Margin="14,300,0,0" Text="Accuracy" Name="textAccuracy" /> </Grid> </UserControl>
You'll need to replace the first part of the class name in the previous snippet with the name of your application's class. For example, if you created a project named "Application1", you'd replace:
<UserControl x:Class="GeolocationSample.MainPage"
with
<UserControl x:Class="Application1.MainPage"
5. Build the app
Choose Build > Build Solution to build the project.
6. Test the app
- On the Debug menu, click Start Debugging to test the solution.
- The first time you run the sample, 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.
Note If location data doesn't display, check the following:
- Make sure you've enabled access to location by opening package.appxmanifest in Solution Explorer and checking Location in the Capabilities tab.
- If an administrator has disabled location services, your app won't be able to access the user's location. In the desktop Control Panel, open Change Location Settings and check if Turn on the Windows Location platform is checked.
Summary and next steps
The previous example demonstrates how little code you'll need to write in order to integrate geolocation in your application.
The application makes an asynchronous request to get geolocation info in the button1_Click method. This occurs on the following line:
IGeoposition pos = await geo.GetGeopositionAsync();
The new geolocation data is captured in the subsequent lines:
textblockLatitude.Text = "Latitude: " + pos.Coordinate.Latitude.ToString(); textblockLongitude.Text = "Longitude: " + pos.Coordinate.Longitude.ToString(); textblockAccuracy.Text = "Accuracy: " + pos.Coordinate.Accuracy.ToString();
These new values are written to the three TextBlocks found in the project's XAML.
<TextBlock Margin="14,220,0,0" Text="Latitude" Name="textLatitude" /> <TextBlock Margin="14,260,0,0" Text="Longitude" Name="textLongitude" /> <TextBlock Margin="14,300,0,0" Text="Accuracy" Name="textAccuracy" />
The next tutorial, How to respond to location updates, shows you how to subscribe to location updates.
Related topics
- Geolocation Sample
- Bing Maps SDK Samples
- Windows.Devices.Geolocation
- Guidelines for using sensitive devices
- Roadmap for creating apps using C#, C++, or VB
Build date: 11/29/2012