在 iOS Xamarin.Forms.Maps 中使用 Xamarin.Essentials 地理定位获取设备位置的奇怪问题

Weird problem getting device's location with Xamarin.Essentials Geolocation in Xamarin.Forms.Maps on iOS

我有一个奇怪的问题 仅 iOS,我正在使用 Xamarin.Forms.Maps(从文档复制粘贴),我的 iOS 模拟器将其模拟位置设置为“Apple”,我正在使用 Xamarin.Essentials 地理定位来获取设备的位置:

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 需要它才能工作

奇怪的是,当我 运行 代码到达 if (location.IsFromMockProvider) 方法中的 Map.MoveToRegion(mapSpan); 时,它转到 CustomMapRendererGetViewForAnnotation 和 运行s var customPin = GetCustomPin(annotation as MKPointAnnotation); 方法就好像它试图获取对我来说没有意义的地图图钉,它抛出 System.Reflection.TargetInvocationException 因为 CustomPin GetCustomPin(MKPointAnnotation annotation)annotation 参数是空的,当然不应该调用它吗?

奇怪的是,对方法的调用:var customPin = GetCustomPin2(annotation as MKPointAnnotation); 实际上将纬度和经度作为参数传递,我很困惑。

它应该首先调用它吗?

有谁知道它为什么这样做?我在 Android 上没有遇到这个问题,目前无法检查它是否在 Android 上有同样的问题,但如果有人知道它为什么会这样以及解决方案是什么,请帮助。

谢谢

编辑

正如 Cole-MSFT 所说,我在 GetCustomPin 方法中添加了一个 null 语句并且它起作用了

CustomPin GetCustomPin(MKPointAnnotation annotation)
    {
        Position position;

        if (annotation != null)
        {
            position = new Position(annotation.Coordinate.Latitude, annotation.Coordinate.Longitude);
            if (position != null)
            {
                foreach (var pin in customPins)
                {
                    if (pin.Position == position)
                    {
                        return pin;
                    }
                }
            }
        }
        else
        {
            return null;
        }

        return null;
    }

如果删除插件,问题是否仍然存在Xamarin.Essentials Geolocation

您在真实设备上试过吗?


看定义(copy from docs)

The GetViewForAnnotation method is called when the location of the annotation becomes visible on the map, and is used to customize the annotation prior to display.

如果您未在地图上设置 Pins,则不应调用此方法。

尝试添加以下代码

if(annotation == null)  return null;  //add this line
var customPin = GetCustomPin(annotation as MKPointAnnotation);