![](/img/trans.png)
[英]How to use Google Maps on iOS using Xamarin.Forms.Maps?
[英]Weird problem getting device's location with Xamarin.Essentials Geolocation in Xamarin.Forms.Maps on iOS
I'm having a weird problem on iOS only , I'm using Xamarin.Forms.Maps ( copy paste from docs ), my iOS simulator has its Mock location to "Apple" and I'm using Xamarin.Essentials Geolocation to get the设备位置:
private async Task ExecuteDisplayCurrentLocationCommand()
{
IsBusy = true;
try
{
MainThread.BeginInvokeOnMainThread(async () =>
{
var request = new GeolocationRequest(GeolocationAccuracy.High, TimeSpan.FromSeconds(10));
var cts = new CancellationTokenSource();
var location = await Geolocation.GetLocationAsync(request, cts.Token);
if (location != null)
{
if (location.IsFromMockProvider)
{
//await App.Current.MainPage.DisplayAlert("Wrong Location", "Your device is giving a false location, please turn on location services in the settings", "Cancel");
Position position1 = new Position(location.Latitude, location.Longitude);
MapSpan mapSpan1 = MapSpan.FromCenterAndRadius(position1, Distance.FromKilometers(8));
Map.MoveToRegion(mapSpan1);
}
Position position = new Position(location.Latitude, location.Longitude);
MapSpan mapSpan = MapSpan.FromCenterAndRadius(position, Distance.FromKilometers(8));
Map.MoveToRegion(mapSpan);
}
//else
//{
// await App.Current.MainPage.DisplayAlert("Can't get location", "", "Cancel");
//}
});
}
catch (FeatureNotSupportedException)
{
await App.Current.MainPage.DisplayAlert("Location feature not supported", "Your device doesn't support location, please use a newer device", "Ok");
}
catch (FeatureNotEnabledException)
{
//await App.Current.MainPage.DisplayAlert("Location isn't enabled", "Your device doesn't have location services enabed, please turn on location services in the settings", "Ok");
}
catch (PermissionException)
{
await App.Current.MainPage.DisplayAlert("Location permission", "Your device isn't giving us permission to use your location, please turn on location services in the settings", "Ok");
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
finally
{
IsBusy = false;
}
}
我正在使用MainThread.BeginInvokeOnMainThread
因为它需要 iOS 才能工作
奇怪的是,当我运行代码并到达Map.MoveToRegion(mapSpan);
在if (location.IsFromMockProvider)
方法中,它转到CustomMapRenderer
的GetViewForAnnotation
并运行var customPin = GetCustomPin(annotation as MKPointAnnotation);
方法好像它试图获取 map 引脚,这对我来说没有意义,这会引发System.Reflection.TargetInvocationException
因为CustomPin GetCustomPin(MKPointAnnotation annotation)
的annotation
参数是 null,当然它不应该调用对的?
奇怪的是对方法的调用: var customPin = GetCustomPin2(annotation as MKPointAnnotation);
实际上将 lat 和 lon 作为参数传递,我很困惑。
它应该首先调用它吗?
有谁知道它为什么这样做? 我在 Android 上没有遇到这个问题,目前无法检查它是否在 Android 上也有同样的问题,但如果有人知道它为什么会这样以及解决方案是什么,请帮忙。
谢谢
如果您删除插件Xamarin.Essentials Geolocation
问题是否仍然存在?
你在真机上试过吗?
查看定义(从文档复制)
当注释的位置在 map 上可见时调用 GetViewForAnnotation 方法,用于在显示之前自定义注释。
如果您没有在 map 上设置Pins
,则不应调用此方法。
尝试添加以下代码
if(annotation == null) return null; //add this line
var customPin = GetCustomPin(annotation as MKPointAnnotation);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.