我想获得用户的位置,因此我使用James Montemagno's Geolocator Plugin for Xamarin。作者在code-behind.cs文件中使用此代码;我把它提取到一个静态类:
public static class GeoService
{
public static Position savedPosition;
public static async Task<Position> GetPosition()
{
var lastknown = await ExecuteLastKnownPosition();
if(lastknown == null)
{
var current = await ExecuteGPSPosition();
if(current != null)
{
return current;
}
else
{
return null;
}
}
return lastknown;
}
private static async Task<Position> ExecuteLastKnownPosition()
{
try
{
var hasPermission = await Utils.CheckPermissions(Permission.Location);
if (!hasPermission)
return null;
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
//Progress Ring einfügen
var position = await locator.GetLastKnownLocationAsync();
if (position == null)
{
//Benachrichtigung über null lastknownLocation
//Aufrufen der CurrentPosition Methode
return null;
}
savedPosition = position;
return position;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
await Application.Current.MainPage.DisplayAlert("Uh oh", "Something went wrong, but don't worry we captured for analysis! Thanks.", "OK");
return null;
}
finally
{
//Freigabe der Buttons und Bools
}
}
private static async Task<Position> ExecuteGPSPosition()
{
try
{
var hasPermission = await Utils.CheckPermissions(Permission.Location);
if (!hasPermission)
return null;
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 100;
//WarteText/Symbol
var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(15));
if (position == null)
{
//Warnung, dass kein GPS vorhanden ist
return null;
}
savedPosition = position;
return position;
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Uh oh", "Something went wrong, but don't worry we captured for analysis! Thanks.", "OK");
return null;
}
finally
{
//Zurücksetzen von Buttons und Lademodus beenden
}
}
public static async Task<Address> ExecuteTrackingAdress(Position currentPosition)
{
try
{
//if (savedPosition == null)
// return null;
var hasPermission = await Utils.CheckPermissions(Permission.Location);
if (!hasPermission)
return null;
string mapkey = "Ajbb9XXXXXXatUzUg1w9BSXXXXXVUAEuF4P-TSXJpnvl5OpXXXXXXXXX";
var locator = CrossGeolocator.Current;
var addresses = await locator.GetAddressesForPositionAsync(currentPosition, mapkey);
var address = addresses.FirstOrDefault();
if (address == null)
{
Debug.WriteLine("Keine Adresse vorhanden");
}
return address;
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Uh oh", "Something went wrong, but don't worry we captured for analysis! Thanks.", "OK");
return null;
}
finally
{
}
}
}
现在我有一个ContentPage,当我转到该页面(PushModalAsync)时,视图模型中的构造函数调用GetPosition()
方法。有一个权限任务,每当任务运行时,UWP都会提示我提示位置权限。
不幸的是,从这一点来看应用程序正在冻结。我无法选择是/否,并且无法进行互动。
我尝试使用Task.WhenAll()
等方法调用方法异步,但每次都会冻结。
这是我在视图模型中编写的最后一个代码
private async void ExecuteGetPosition()
{
IsBusy = true;
await Task.Yield();
var positiontask = GeoService.GetPosition();
var addresstask = GeoService.ExecuteTrackingAdress(positiontask.Result);
await Task.WhenAll(positiontask, addresstask);
CurrentPosition = positiontask.Result;
CurrentAddress = addresstask.Result;
OnPropertyChanged("CurrentAddressView");
IsBusy = false;
}
我假设XAML ContentPage没有正确加载,因此提示窗口会在&#34;后面滑动&#34; MainWindow或其他什么。
请你能给我一个解决方法来解决这个问题吗?
修改 将这些行添加到我的App.xaml.cs OnStart() - 方法带来了解决方案。 Windows现在正在调用OnStart权限,Android要求获得gps-request权限...疯狂:
var permission = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);
答案 0 :(得分:0)
你在iOS上运行吗?如果是这样,请查看模拟器的控制台日志以查看是否存在隐私错误。我猜它与请求位置权限时要求在info.plist中的隐私声明有关。
查看列出here所列的所有必要隐私声明。
它们包括(查看链接以获取有关何时添加每个的信息):
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access location when open.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access location when in the background.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access location when open and in the background.</string>