【问题标题】:C# UWP Geolocation, access deniedC# UWP 地理位置,访问被拒绝
【发布时间】:2017-01-05 08:23:12
【问题描述】:

大家。

我正在尝试在 C# UWP 应用程序中获取当前用户的位置。

var geoLocator = new Geolocator();
geoLocator.DesiredAccuracy = PositionAccuracy.High;
var accessStatus = await Geolocator.RequestAccessAsync();
var pos = await geoLocator.GetGeopositionAsync();
var latitude = pos.Coordinate.Point.Position.Latitude;
var longitude = pos.Coordinate.Point.Position.Longitude;

但我得到了错误:

“System.UnauthorizedAccessException”类型的异常发生在 mscorlib.ni.dll 中,但未在用户代码中处理 WinRT 信息:您的应用无权访问位置数据。确保您已在应用程序清单中定义了 ID_CAP_LOCATION,并且在您的手机上,您已通过检查设置 > 位置打开位置。 附加信息:访问被拒绝。

在 Package.appxmanifest 我检查了这些项目: Internet(客户端和服务器)-选中, 互联网(客户端) - 检查, 位置 - 已检查。

如何在本地机器上设置权限?

【问题讨论】:

  • "...而且您的手机上,您已通过检查设置>位置来打开位置",您检查了吗?
  • 我在本地机器上调试它……在模拟器中,而不是在手机上。

标签: c# location uwp coordinates access-denied


【解决方案1】:

我解决了这个问题。在 Windows 10 中,在位置隐私设置中,我的位置服务未打开。所以我打开了它,现在它可以工作了。

【讨论】:

    【解决方案2】:

    您需要检查是否允许您访问定位服务? 您可以使用 UWP 中 GeoLocater 类的 RequestAccessAsync() 方法处理此问题。

            Geolocator geoLocator = new Geolocator();
            GeolocationAccessStatus accessStatus = await Geolocator.RequestAccessAsync();
    
            if ( accessStatus == GeolocationAccessStatus.Allowed)
            {
                // Put all your Code here to access location services
                Geoposition geoposition = await geoLocator.GetGeopositionAsync();
                var position = geoposition.Coordinate.Point.Position;
                var latlong = string.Format("lat:{0}, long:{1}", position.Latitude, position.Longitude);
            }
            else if (accessStatus == GeolocationAccessStatus.Denied)
            {
                   // No Accesss
            }
            else
            {
            }
    

    【讨论】:

      猜你喜欢
      • 2021-09-18
      • 1970-01-01
      • 1970-01-01
      • 2018-05-17
      • 2019-06-28
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 2016-08-12
      相关资源
      最近更新 更多