C# Windows Universal App 10中的地理位置

C# Windows Universal App 10中的地理位置,c#,windows,geolocation,C#,Windows,Geolocation,我正在尝试获取设备的地理位置,如下所示: Geolocator geo = new Geolocator(); double lat = 0, longt = 0; Task getPosition = Task.Run(async () => { try { Geoposition pos = await geo.GetGeopositionA

我正在尝试获取设备的地理位置,如下所示:

        Geolocator geo = new Geolocator();
        double lat = 0, longt = 0;

        Task getPosition = Task.Run(async () =>
        {
            try
            {
                Geoposition pos = await geo.GetGeopositionAsync();
                lat = pos.Coordinate.Point.Position.Latitude;
                longt = pos.Coordinate.Point.Position.Longitude;
            }
            catch(Exception exp)
            {
                Debug.WriteLine(exp);
            }

        });
        getPosition.Wait();
但是,我得到了以下例外:

你的应用没有访问位置数据的权限。确保 您已经在应用程序清单中定义了ID\u CAP\u位置,并且 在手机上,您已通过检查设置打开位置> 地点

  • 使用Windows 10在我的笔记本电脑上进行测试
  • 位置已在我的笔记本电脑上激活
  • 我在清单中添加了定位功能

你知道有什么问题吗?谢谢大家!

您提到了笔记本电脑,但错误消息是针对手机的。您是否在手机的模拟器中运行此功能,并在手机中打开了它?如果没有,只需在“设置->位置”中打开它。如果没有,请将microsoft的project adamt发送给我,我将在此处复制、修复并发布更新,或者尝试在我的端复制


编辑-我看过这个项目 解决方案: 从Windows 10开始,您需要包括Geolocator.RequestAccessAsync()。这将提示用户允许位置请求。在您的特定示例中,请确保使用async标记您的按钮1\u单击:

private async void Button_Click(object sender, RoutedEventArgs e) 专用异步无效按钮\u单击(对象发送方,路由目标) 从

var accessStatus=await geologitor.RequestAccessAsync(); 开关(访问状态) { 案例地理位置访问状态。允许: _NotifyUser(“等待更新…”,NotifyType.StatusMessage); //如果未设置DesiredAccuracy或DesiredAccuracyNames(或值为0),则使用DesiredAccuracy.Default。 Geolocator Geolocator=新的Geolocator{DesiredAccuracyInMeters=\u DesiredAccuracyInMetersValue}; //订阅StatusChanged事件以获取位置状态更改的更新 _geolocator.StatusChanged+=OnStatusChanged; //执行操作 Geoposition pos=等待地理定位器。GetGeopositionAsync(); 更新定位数据(pos); _NotifyUser(“位置已更新”,NotifyType.StatusMessage); 打破 案例地理位置访问状态。拒绝: _NotifyUser(“对位置的访问被拒绝。”,NotifyType.ErrorMessage); LocationDisabledMessage.Visibility=可见性.Visibility; UpdateLocationData(空); 打破 案例地理位置访问状态。未指定: _NotifyUser(“未指定的错误”,NotifyType.ErrorMessage); UpdateLocationData(空); 打破 }
我以本地机器而不是模拟器的形式运行这个项目。我怎样才能把项目寄给你?谢谢给我发邮件到微软的adamt OK,我已经把项目发给了微软的adamt.com,如果你收到了,请告诉我。非常感谢,它成功了!我对代码只有一个疑问。你能给我解释一下发生了什么变化吗?它似乎不存在具有该名称的变量,我不知道它的用途是什么。这是一个在状态更改时得到通知的事件处理程序。如果您键入_geologitor.StatusChange+=我相信,那么您将自动创建事件处理程序。它是可选的,只是为了获得更改通知,如果您愿意,您可以将其注释掉 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; }