Don*_*itz 7 c# xaml geolocation win-universal-app uwp
Windows Universal(Windows 10应用程序)中的Geolocation新API提供了一种允许访问用户位置的新方法.
从Windows 10开始,在访问用户的位置之前调用RequestAccessAsync方法.那时,您的应用必须位于前台,并且必须从UI线程调用RequestAccessAsync.
我在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;
}
}
Run Code Online (Sandbox Code Playgroud)
它被称为:
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
ForecastViewModel vm = await ForecastViewModel.BuildViewModelAsync(await GetPositionAsync());
DataContext = vm.Forecast;
uxForecastList.Visibility = Visibility.Visible;
}
Run Code Online (Sandbox Code Playgroud)
Dan*_*ner 12
您必须设置"位置"功能.你可以在appmanifest中做到这一点.
在此处查找更多信息:
https://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.geolocation.geolocator.aspx(向下滚动以查找有关功能的信息)
| 归档时间: |
|
| 查看次数: |
5049 次 |
| 最近记录: |