[英]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.