Dela via


Geolocator Class

Definition

Provides access to the current geographic location.

public ref class Geolocator sealed
/// [Windows.Foundation.Metadata.Activatable(65536, Windows.Foundation.UniversalApiContract)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class Geolocator final
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
/// [Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
class Geolocator final
[Windows.Foundation.Metadata.Activatable(65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class Geolocator
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
public sealed class Geolocator
function Geolocator()
Public NotInheritable Class Geolocator
Inheritance
Object Platform::Object IInspectable Geolocator
Attributes

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)
App capabilities
location ID_CAP_LOCATION [Windows Phone]

Examples

This example shows how to use the Geolocator class to retrieve the device's location. For more info, see Get current location.

using Windows.Devices.Geolocation;
...
var accessStatus = await Geolocator.RequestAccessAsync();
switch (accessStatus)
{
    case GeolocationAccessStatus.Allowed:
        // notify user: Waiting for update

        // 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);
        // notify user: Location updated
        break;

    case GeolocationAccessStatus.Denied:
        // notify user: Access to location is denied

        break;

    case GeolocationAccessStatus.Unspecified:
        // notify user: Unspecified error
        break;
}

Remarks

Version history

Windows version SDK version Value added
1607 14393 AllowFallbackToConsentlessPositions
1607 14393 DefaultGeoposition
1607 14393 IsDefaultGeopositionRecommended

Constructors

Geolocator()

Initializes a new Geolocator object.

Properties

DefaultGeoposition

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

DesiredAccuracy

The accuracy level at which the Geolocator provides location updates.

DesiredAccuracyInMeters

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

IsDefaultGeopositionRecommended

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

LocationStatus

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

MovementThreshold

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

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.

Methods

AllowFallbackToConsentlessPositions()

Sets the Geolocator to use coarse location as a fallback option (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, TimeSpan)

Important

The Geolocator.GetGeopositionHistoryAsync method is deprecated, and it may not be available in future versions of Windows.

Starts an asynchronous operation to retrieve the location history of the device.

Note

This API is not available to all Windows apps. Unless your developer account is specially provisioned by Microsoft, calls to these APIs will fail at runtime.

GetGeopositionHistoryAsync(DateTime)

Important

The Geolocator.GetGeopositionHistoryAsync method is deprecated, and it may not be available in future versions of Windows.

Starts an asynchronous operation to retrieve the location history of the device.

Note

This API is not available to all Windows apps. Unless your developer account is specially provisioned by Microsoft, calls to these APIs will fail at runtime.

RequestAccessAsync()

Note

Some information relates to pre-released product, which may be substantially modified before it's commercially released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Important

This API will be affected by upcoming changes to operating system behavior, planned for fall 2024. For more info, see Changes to API behavior for Wi-Fi access and location.

Requests permission to access location data.

Events

PositionChanged

Raised when the location is updated.

StatusChanged

Raised when the ability of the Geolocator to provide updated location changes.

Applies to

See also