Plugin.Geolocator 退出方法(死锁?)
Plugin.Geolocator exits method (deadlock?)
我正在构建一个 Xamarin 应用程序,对于地理定位,我正在使用 GeolocatorPlugin
问题是一旦代码想要获取位置,代码就存在而没有警告。
我的 class 字段:
private Position position;
private IGeolocator locator = CrossGeolocator.Current;
我的页面构造函数:
public MainPage()
{
InitializeComponent();
locator.PositionChanged += Locator_PositionChanged;
locator.PositionError += Locator_PositionError;
}
OnAppearing 事件正在调用 getLocationPermission:
private async Task GetLocationPermission()
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.LocationWhenInUse);
if (status != PermissionStatus.Granted)
{
//Not granted, request permission
if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.LocationWhenInUse))
{
// This is not the actual permission request
await DisplayAlert("Need your permission", "We need to access your location", "Ok");
}
// This is the actual permission request
var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.LocationWhenInUse);
if (results.ContainsKey(Permission.LocationWhenInUse))
status = results[Permission.LocationWhenInUse];
}
//Already granted, go on
if (status == PermissionStatus.Granted)
{
//Granted, get the location
GetLocation();
await GetVenues();
await locator.StartListeningAsync(TimeSpan.FromMinutes(30), 500);
}
else
{
await DisplayAlert("Access to location denied", "We don't have access to your location.", "OK");
}
}
权限被授予并获取到GetLocation()方法:
private async void GetLocation()
{
//var locator = CrossGeolocator.Current;
try
{
var myPosition = await locator.GetPositionAsync();
position = new Position(myPosition.Latitude, myPosition.Longitude);
}
catch (Exception ex)
{
throw;
}
if (position == null)
{
//Handle exception
}
}
一旦使用 locator.GetPositionAsync() 到达该行,它就会停止。不会抛出异常,也不会引发 PositionError。
不知道为什么,一开始用了一次,之后就没用了。
de Android模拟器中的location设置如下:
根据我的研究,您没有像 this link
那样 Location Changes
我写了一个关于 Location changes
的演示。这是 运行 屏幕截图。
我正在构建一个 Xamarin 应用程序,对于地理定位,我正在使用 GeolocatorPlugin
问题是一旦代码想要获取位置,代码就存在而没有警告。
我的 class 字段:
private Position position;
private IGeolocator locator = CrossGeolocator.Current;
我的页面构造函数:
public MainPage()
{
InitializeComponent();
locator.PositionChanged += Locator_PositionChanged;
locator.PositionError += Locator_PositionError;
}
OnAppearing 事件正在调用 getLocationPermission:
private async Task GetLocationPermission()
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.LocationWhenInUse);
if (status != PermissionStatus.Granted)
{
//Not granted, request permission
if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.LocationWhenInUse))
{
// This is not the actual permission request
await DisplayAlert("Need your permission", "We need to access your location", "Ok");
}
// This is the actual permission request
var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.LocationWhenInUse);
if (results.ContainsKey(Permission.LocationWhenInUse))
status = results[Permission.LocationWhenInUse];
}
//Already granted, go on
if (status == PermissionStatus.Granted)
{
//Granted, get the location
GetLocation();
await GetVenues();
await locator.StartListeningAsync(TimeSpan.FromMinutes(30), 500);
}
else
{
await DisplayAlert("Access to location denied", "We don't have access to your location.", "OK");
}
}
权限被授予并获取到GetLocation()方法:
private async void GetLocation()
{
//var locator = CrossGeolocator.Current;
try
{
var myPosition = await locator.GetPositionAsync();
position = new Position(myPosition.Latitude, myPosition.Longitude);
}
catch (Exception ex)
{
throw;
}
if (position == null)
{
//Handle exception
}
}
一旦使用 locator.GetPositionAsync() 到达该行,它就会停止。不会抛出异常,也不会引发 PositionError。
不知道为什么,一开始用了一次,之后就没用了。
de Android模拟器中的location设置如下:
根据我的研究,您没有像 this link
那样Location Changes
我写了一个关于 Location changes
的演示。这是 运行 屏幕截图。