Export (0) Print
Expand All
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Quickstart: Detecting a user's location (XAML)

Applies to Windows and Windows Phone

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

Open Microsoft Visual Studio.

2. Create a new project

Create a new project, choosing an Application from the Visual C# > Store project types.

3. Enable the location capability

Open your project's Package.appxmanifest file, select the Capabilities tab, and then enable the Location capability by placing a checkmark in the corresponding check box.

4. Replace the C# code

Open your project's MainPage.xaml.cs file and replace the existing code with the following.


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
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;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/p/?LinkID=234238

namespace App1
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        Geolocator geo = null;
        public MainPage()
        {
            this.InitializeComponent();
        }
    
        private async void button1_Click(
            object sender, RoutedEventArgs e)
        {
            if (geo == null)
            {
                geo = new Geolocator();
            }

            Geoposition pos = await geo.GetGeopositionAsync();
            textLatitude.Text = "Latitude: " + pos.Coordinate.Point.Position.Latitude.ToString();
            textLongitude.Text = "Longitude: " + pos.Coordinate.Point.Position.Longitude.ToString();
            textAccuracy.Text = "Accuracy: " + pos.Coordinate.Accuracy.ToString();
        }

    }
}



You'll need to rename the namespace in the previous snippet with the name you gave your project. For example, if you created a project named GeolocationSample, you'd replace namespace App1 with namespace GeolocationSample.

5. Replace the XAML code

Open the file MainPage.xaml and copy the following XML into this file (replacing its original contents).


<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Button Content="Get Location Async" Click="button1_Click" 
            HorizontalAlignment="Left" Margin="11,164,0,0" Name="button1" VerticalAlignment="Top" />
        <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>
</Page>


You'll need to replace the first part of the class name in the previous snippet with the namespace of your app. For example, if you created a project named GeolocationSample, you'd replace x:Class="App1.MainPage" with x:Class="GeolocationSample.MainPage". You should also replace xmlns:local="using:App1" with xmlns:local="using:GeolocationSample".

6. Build the app

Choose Build > Build Solution to build the project.

7. Test the app

  1. On the Debug menu, click Start Debugging to test the solution.
  2. On Windows, 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.
  3. 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.
  • On Windows, 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.
  • On Windows Phone, the user can disable location in the Settings app. Go to the Settings app, select location and make sure that the Location services switch is toggled on.

Summary

The previous example demonstrates how little code you'll need to write in order to integrate geolocation in your app.

The app makes an asynchronous request to get geolocation info in the button1_Click method. This occurs on the following line of code.


            Geoposition pos = await geo.GetGeopositionAsync();

The new geolocation data is captured in the subsequent lines.


            textLatitude.Text = "Latitude: " + pos.Coordinate.Point.Position.Latitude.ToString();
            textLongitude.Text = "Longitude: " + pos.Coordinate.Point.Position.Longitude.ToString();
            textAccuracy.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" />

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

 

 

Show:
© 2014 Microsoft