通用的Windows(UWP)地理位置API权限 [英] Windows Universal (UWP) Geolocation API Permissions

查看:594
本文介绍了通用的Windows(UWP)地理位置API权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关地理位置在Windows通用(视窗10应用程序),新的API有允许访问用户的位置的新方法。




在Windows 10开始,调用RequestAccessAsync方法访问用户的位置之前。在那个时候,您的应用程序必须在前台和RequestAccessAsync必须从UI线程调用。




我跑了一些非常如下所示,地理位置简单的代码,在UI线程,但我得到的位置的权限被拒绝每次和任何提示,让位置的权限。有没有其他人碰到这个? 如何,我得到的提示,以便在Windows 10应用地理位置定位的权限?



地理位置法

 专用异步任务< ForecastRequest> GetPositionAsync()
{

{

//请求权限访问的位置
VAR accessStatus =等待Geolocator.RequestAccessAsync();

如果(accessStatus == GeolocationAccessStatus.Allowed)
{
//获取取消标记
_cts =新CancellationTokenSource();
的CancellationToken记号= _cts.Token;

//如果未设置DesiredAccuracy或DesiredAccuracyInMeters(或值为0),则使用DesiredAccuracy.Default。
Geolocator geolocator =新Geolocator {DesiredAccuracyInMeters = _desireAccuracyInMetersValue};

//进行操作
_pos =等待geolocator.GetGeopositionAsync()AsTask(标记)。

返回新ForecastRequest()
{
纬度=(浮点)_pos.Coordinate.Point.Position.Latitude,
经度=(浮点)_pos.Coordinate。 Point.Position.Longitude,
股= Common.Unit.us
};
}
,否则
抛出新的异常(问题与位置权限或访问);

}
赶上(TaskCanceledException TCE)
{
抛出新的异常(任务取消+ tce.Message);
}
终于
{
_cts = NULL;
}
}



如果这就是所谓的:

 保护异步覆盖无效的OnNavigatedTo(NavigationEventArgs E)
{
base.OnNavigatedTo(E);

ForecastViewModel VM =等待ForecastViewModel.BuildViewModelAsync(等待GetPositionAsync());
的DataContext = vm.Forecast;

uxForecastList.Visibility = Visibility.Visible;
}


解决方案

您必须设置位置的能力。你可以在appmanifest做到这一点。



在截图中,你找到在哪里设置的能力:



在这里找到更多的信息:



https://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.geolocation .geolocator.aspx (滚动下来的所有上找到的信息的能力)


The new API for Geolocation in Windows Universal (Windows 10 apps) has a new way for allowing access to a user's location.

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.

I'm running some very simple code for Geolocation, on the UI thread as shown below, but I get location permission "denied" every time and no prompt to allow location permissions. Has anyone else run into this? How do I get the prompt to allow location permissions for geolocation in a Windows 10 app?

Geolocation method

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;
        }
    }

Where it's called:

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

        ForecastViewModel vm = await ForecastViewModel.BuildViewModelAsync(await GetPositionAsync());
        DataContext = vm.Forecast;

        uxForecastList.Visibility = Visibility.Visible;
    }

解决方案

You have to set the "Location" capability. You can do this in the appmanifest.

In the screenshot you find where to set the capability:

Find more info here:

https://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.geolocation.geolocator.aspx (Scroll all down to find info on capabilities)

这篇关于通用的Windows(UWP)地理位置API权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆