微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – Windows Universal(UWP)地理位置API权限

Windows Universal(Windows 10应用程序)中的Geolocation新API提供了一种允许访问用户位置的新方法.

Starting in Windows 10,call the RequestAccessAsync method before accessing the user’s location. At that time,your app must be in the foreground and RequestAccessAsync must be called from the UI thread.

我在UI线程上运行一些非常简单的Geolocation代码,如下所示,但我每次都获得“拒绝”的位置权限,并且没有提示允许位置权限.有没有其他人遇到这个?如何获得在Windows 10应用程序中允许地理位置的位置权限的提示

地理定位方法

private async Task<forecastrequest> GetPositionAsync()
    {
        try
        {

            // Request permission to access location
            var accessstatus = await Geolocator.RequestAccessAsync();

            if (accessstatus == GeolocationAccessstatus.Allowed)
            {
                // Get cancellation token
                _cts = new CancellationTokenSource();
                CancellationToken token = _cts.Token;

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

                // Carry out the operation
                _pos = await geolocator.GetGeopositionAsync().AsTask(token);

                return new forecastrequest()
                {
                    Lat = (float)_pos.Coordinate.Point.Position.Latitude,Lon = (float)_pos.Coordinate.Point.Position.Longitude,Unit = Common.Unit.us
                };
            }
            else
                throw new Exception("Problem with location permissions or access");

        }
        catch (TaskCanceledException tce)
        {
            throw new Exception("Task cancelled" + tce.Message);
        }
        finally
        {
            _cts = null;
        }
    }

它被称为:

protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        Forecastviewmodel vm = await Forecastviewmodel.BuildviewmodelAsync(await GetPositionAsync());
        DataContext = vm.Forecast;

        uxForecastList.Visibility = Visibility.Visible;
    }

解决方法

您必须设置“位置”功能.你可以在appmanifest中做到这一点.

在屏幕截图中,您可以找到设置功能的位置:

在此处查找更多信息:

https://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.geolocation.geolocator.aspx(向下滚动以查找功能信息)

原文地址:https://www.jb51.cc/csharp/244340.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐