共用方式為


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

相對於上一個 PositionChanged 事件座標的移動距離, 這是 Geolocator 引發 PositionChanged 事件所需的距離。

ReportInterval

位置更新之間要求的最小時間間隔,以毫秒為單位。 如果您的應用程式不常需要更新,請設定此值,讓位置服務只在需要時計算位置,以節省電源。

方法

AllowFallbackToConsentlessPositions()

Sets the Geolocator to use coarse location as a fallback option (see Remarks).

GetGeopositionAsync()

啟動非同步作業以擷取裝置的目前位置。

GetGeopositionAsync(TimeSpan, TimeSpan)

啟動非同步作業以擷取裝置的目前位置。

GetGeopositionHistoryAsync(DateTime)

啟動非同步作業以擷取裝置的位置歷程記錄。

注意

此 API 不適用於所有 Windows 應用程式。 除非您的開發人員帳戶是由 Microsoft 特別布建,否則呼叫這些 API 會在執行時間失敗。

GetGeopositionHistoryAsync(DateTime, TimeSpan)

啟動非同步作業以擷取裝置的位置歷程記錄。

注意

此 API 不適用於所有 Windows 應用程式。 除非您的開發人員帳戶是由 Microsoft 特別布建,否則呼叫這些 API 會在執行時間失敗。

RequestAccessAsync()

注意

針對發行前產品的部分相關資訊,在產品正式發行時可能會有大幅修改。 針對此處提供的資訊,Microsoft 不做任何明示或默許的擔保。

重要

此 API 將受到即將變更作業系統行為所影響,其計畫為 2024 年 2024 年。 如需詳細資訊,請參閱 Wi-Fi 存取和位置的 API 行為變更

要求存取位置資料的許可權。

事件

PositionChanged

更新位置時引發。

StatusChanged

當 Geolocator提供更新的位置變更的能力時引發。

適用於

另請參閱