Geolocator class

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
AllowFallbackToConsentlessPositions Sets the Geolocator to use Consentless Location, in which position requests will return an obfuscated location when the circumstances call for it (see Remarks).
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

DefaultGeoposition

Read/write

Gets the location manually entered into the system by the user, to be utilized if no better options exist.

DesiredAccuracy

Read/write

The accuracy level at which the Geolocator provides location updates.

DesiredAccuracyInMeters

Read/write

Gets or sets the desired accuracy in meters for data returned from the location service.

IsDefaultGeopositionRecommended

Read-only

Indicates whether the user should be prompted to set a default location manually.

LocationStatus

Read-only

The status that indicates the ability of the Geolocator to provide location updates.

MovementThreshold

Read/write

Gets 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/write

The 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. For more info, see Get current location.


using Windows.Devices.Geolocation;
...
var accessStatus = await Geolocator.RequestAccessAsync();
switch (accessStatus)
{
    case GeolocationAccessStatus.Allowed:
        _rootPage.NotifyUser("Waiting for update...", NotifyType.StatusMessage);

        // If DesiredAccuracy or DesiredAccuracyInMeters are not set (or value is 0), DesiredAccuracy.Default is used.
        Geolocator geolocator = new Geolocator { DesiredAccuracyInMeters = _desireAccuracyInMetersValue };

        // Subscribe to StatusChanged event to get updates of location status changes
        _geolocator.StatusChanged += OnStatusChanged;
                        
        // Carry out the operation
        Geoposition pos = await geolocator.GetGeopositionAsync();

        UpdateLocationData(pos);
        _rootPage.NotifyUser("Location updated.", NotifyType.StatusMessage);
        break;

    case GeolocationAccessStatus.Denied:
        _rootPage.NotifyUser("Access to location is denied.", NotifyType.ErrorMessage);
        LocationDisabledMessage.Visibility = Visibility.Visible;
        UpdateLocationData(null);
        break;

    case GeolocationAccessStatus.Unspecified:
        _rootPage.NotifyUser("Unspecified error.", NotifyType.ErrorMessage);
        UpdateLocationData(null);
        break;
}

Requirements (Windows 10 device family)

Device family

Universal, introduced version 10.0.10240.0

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
Get current location
geolocation sample
Geotag sample (Windows 10)

 

 

Show:
© 2016 Microsoft