Geolocator 类

定义

提供对当前地理位置的访问。

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
继承
Object Platform::Object IInspectable Geolocator
属性

Windows 要求

设备系列
Windows 10 (在 10.0.10240.0 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v1.0 中引入)
应用功能
location ID_CAP_LOCATION [Windows Phone]

示例

此示例演示如何使用 Geolocator 类检索设备的位置。 有关详细信息,请参阅获取当前位置

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;
}

注解

版本历史记录

Windows 版本 SDK 版本 增值
1607 14393 AllowFallbackToConsentlessPositions
1607 14393 DefaultGeoposition
1607 14393 IsDefaultGeopositionRecommended

构造函数

Geolocator()

初始化新的 Geolocator 对象。

属性

DefaultGeoposition

获取用户手动输入到系统中的位置,如果没有更好的选项,将利用该位置。

DesiredAccuracy

Geolocator 提供位置更新的准确性级别。

DesiredAccuracyInMeters

获取或设置从定位服务返回的数据所需的精度(以米为单位)。

IsDefaultGeopositionRecommended

指示是否应提示用户手动设置默认位置。

LocationStatus

指示 Geolocator 提供位置更新的能力的状态。

MovementThreshold

Geolocator 引发 PositionChanged 事件所需的相对于最后一个 PositionChanged 事件的坐标的移动距离(以米为单位)。

ReportInterval

请求的位置更新之间的最短时间间隔(以毫秒为单位)。 如果应用程序不经常需要更新,请设置此值,以便定位服务仅在需要时通过计算位置来节省电源。

方法

AllowFallbackToConsentlessPositions()

Geolocator 设置为使用粗糙位置作为回退选项 (请参阅备注) 。

GetGeopositionAsync()

启动异步操作以检索设备的当前位置。

GetGeopositionAsync(TimeSpan, TimeSpan)

启动异步操作以检索设备的当前位置。

GetGeopositionHistoryAsync(DateTime)

启动异步操作以检索设备的位置历史记录。

注意

此 API 并非适用于所有 Windows 应用。 除非你的开发人员帐户由 Microsoft 专门预配,否则对这些 API 的调用将在运行时失败。

GetGeopositionHistoryAsync(DateTime, TimeSpan)

启动异步操作以检索设备的位置历史记录。

注意

此 API 并非适用于所有 Windows 应用。 除非你的开发人员帐户由 Microsoft 专门预配,否则对这些 API 的调用将在运行时失败。

RequestAccessAsync()

注意

一些信息与预发行产品相关,相应产品在商业发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。

重要

此 API 将受到即将于 2024 年秋季进行的操作系统行为更改的影响。 有关详细信息,请参阅 更改 Wi-Fi 访问和位置的 API 行为

请求访问位置数据的权限。

事件

PositionChanged

在更新位置时引发。

StatusChanged

Geolocator 提供更新的位置的能力更改时引发。

适用于

另请参阅