Geolocation.GetGeopositionAsync()

时间:2015-09-18 06:55:55

标签: c# geolocation windows-10

我试图通过Windows 10应用程序获取设备的当前位置。但这会引发标题中显示的异常。有人帮我解决这个问题。

public sealed partial class MainPage : Page
{
    private readonly uint? _desireAccuracyInMetersValue = 10;
    private Geolocator _geolocator = new Geolocator();

    public MainPage()
    {
        this.InitializeComponent();
        _geolocator.MovementThreshold = 1;
        _geolocator.DesiredAccuracy = PositionAccuracy.High;

    }

    private async void StartTracking(object sender, RoutedEventArgs e)
    {
        var accessStatus = await Geolocator.RequestAccessAsync();
        switch (accessStatus)
        {
            case GeolocationAccessStatus.Allowed:
                debugLog("Waiting for update... 1");

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

                debugLog("Waiting for update... 2");
                // Subscribe to StatusChanged event to get updates of location status changes
                _geolocator.StatusChanged += OnStatusChanged;
                debugLog("Waiting for update... 3");
                // Carry out the operation
                Geoposition pos = await geolocator.GetGeopositionAsync();
                debugLog("Waiting for update... 4");
                UpdateLocationData(pos);
                debugLog("Location updated.");
                break;

            case GeolocationAccessStatus.Denied:
                debugLog("Access to location is denied.");
                LocationDisabledMessage.Visibility = Visibility.Visible;
                UpdateLocationData(null);
                break;

            case GeolocationAccessStatus.Unspecified:
                debugLog("Unspecified error.");
                UpdateLocationData(null);
                break;
        }
    }
}

0 个答案:

没有答案