Windows Dev Center

Geolocator class

Provides access to the current geographic location.

Syntax


public sealed class Geolocator

Members

The Geolocator class has these types of members:

Constructors

The Geolocator class has these constructors.

ConstructorDescription
Geolocator Initializes a new Geolocator object.

 

Events

The Geolocator class has these events.

EventDescription
PositionChanged Raised when the location is updated.
StatusChanged Raised when the ability of the Geolocator to provide updated location changes.

 

Methods

The Geolocator class has these methods. With C#, Visual Basic, and C++, it also inherits methods from the Object class.

MethodDescription
GetGeopositionAsync Starts an asynchronous operation to retrieve the current location of the device.
GetGeopositionAsync(TimeSpan,TimeSpan) Starts an asynchronous operation to retrieve the current location of the device.
GetGeopositionHistoryAsync(DateTime) Starts an asynchronous operation to retrieve the location history of the device.
GetGeopositionHistoryAsync(DateTime,TimeSpan) Starts an asynchronous operation to retrieve the location history of the device.
RequestAccessAsync Requests permission to access location data.

 

Properties

The Geolocator class has these properties.

PropertyAccess typeDescription

DesiredAccuracy

Read/writeThe accuracy level at which the Geolocator provides location updates.

DesiredAccuracyInMeters

Read/writeGets or sets the desired accuracy in meters for data returned from the location service.

LocationStatus

Read-onlyThe status that indicates the ability of the Geolocator to provide location updates.

MovementThreshold

Read/writeGets and sets the distance of movement, in meters, relative to the coordinate from the last PositionChanged event, that is required for the Geolocator to raise a PositionChanged event.

ReportInterval

Read/writeThe requested minimum time interval between location updates, in milliseconds. If your application requires updates infrequently, set this value so that location services can conserve power by calculating location only when needed.

 

Remarks

The DesiredAccuracyInMeters property provides more granularity and control of the accuracy of the position results. Most applications can simply use the DesiredAccuracy property.

Important  

Starting in Windows 10, call the RequestAccessAsync before accessing the user’s location. At that time, your app must be in the foreground and RequestAccessAsync must be called from the UI thread. Until the user grants your app permission to their location, your app can't access location data.

Examples

This example shows how to call GetGeopositionAsync.


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
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.UI.Core;
using Windows.Devices.Geolocation;
using System.Threading;
using System.Threading.Tasks;

namespace GeolocationApp
{
    public sealed partial class MainPage : Page
    {
        private Geolocator geolocator = null;

        public MainPage()
        {
            this.InitializeComponent();
            geolocator = new Geolocator();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            btnGetLocation.IsEnabled = true;
        }


        async private void btnGetLocation_Click(object sender, RoutedEventArgs e)
        {
            btnGetLocation.IsEnabled = false;

            try
            {
                // Carry out the operation
                Geoposition pos = await geolocator.GetGeopositionAsync();

                tbLatitude.Text = pos.Coordinate.Latitude.ToString();
                tbLongitude.Text = pos.Coordinate.Longitude.ToString();
                tbAccuracy.Text = pos.Coordinate.Accuracy.ToString();
                tbStatus.Text = GetStatusString(geolocator.LocationStatus);
            }
            catch (System.UnauthorizedAccessException)
            {
                tbLatitude.Text = "No data";
                tbLongitude.Text = "No data";
                tbAccuracy.Text = "No data";
                tbStatus.Text = "No data";
            }
            catch (TaskCanceledException)
            {
                tbLatitude.Text = "Cancelled";
                tbLongitude.Text = "Cancelled";
                tbAccuracy.Text = "Cancelled";
                tbStatus.Text = "Cancelled";
            }

            btnGetLocation.IsEnabled = true;
        }


         private string GetStatusString(PositionStatus status)
        {
                var strStatus = "";

                switch (status)
                {
                    case PositionStatus.Ready:
                        strStatus = "Location is available.";
                        break;

                    case PositionStatus.Initializing:
                        strStatus = "Geolocation service is initializing.";
                        break;

                    case PositionStatus.NoData:
                        strStatus = "Location service data is not available.";
                        break;

                    case PositionStatus.Disabled:
                        strStatus = "Location services are disabled. Use the " +
                                    "Settings charm to enable them.";
                        break;

                    case PositionStatus.NotInitialized:
                        strStatus = "Location status is not initialized because " +
                                    "the app has not yet requested location data.";
                        break;

                    case PositionStatus.NotAvailable:
                        strStatus = "Location services are not supported on your system.";
                        break;

                    default:
                        strStatus = "Unknown PositionStatus value.";
                        break;
                }

                return (strStatus);

        }

    }
}



Requirements (Windows 10 device family)

Device family

Universal

API contract

Windows.Foundation.UniversalApiContract, introduced version 1.0

Namespace

Windows.Devices.Geolocation
Windows::Devices::Geolocation [C++]

Metadata

Windows.Foundation.UniversalApiContract.winmd

Capabilities

location

Requirements (Windows 8.x and Windows Phone 8.x)

Minimum supported client

Windows 8 [Windows Store apps, desktop apps]

Minimum supported server

None supported

Minimum supported phone

Windows Phone 8

Namespace

Windows.Devices.Geolocation
Windows::Devices::Geolocation [C++]

Metadata

Windows.winmd

Capabilities

location
ID_CAP_LOCATION [Windows Phone]

Attributes

[DualApiPartition()]
[MarshalingBehavior(Agile)]
[Muse()]
[Threading(Both)]
[Version(0x06020000)]

See also

Object
Detect a user's location
Set up a geofence
geolocation sample

 

 

Show:
© 2015 Microsoft