简体   繁体   English

使用 Xamarin.Forms.Maps 上 Xamarin.Forms21920411ZCBD85089

[英]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 device's location: 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;
        }
    }

I'm using MainThread.BeginInvokeOnMainThread because it is required on iOS for it to work我正在使用MainThread.BeginInvokeOnMainThread因为它需要 iOS 才能工作

What's weird is that when I run the code and it reaches the Map.MoveToRegion(mapSpan);奇怪的是,当我运行代码并到达Map.MoveToRegion(mapSpan); in the if (location.IsFromMockProvider) method, it goes to the CustomMapRenderer 's GetViewForAnnotation and runs the var customPin = GetCustomPin(annotation as MKPointAnnotation);if (location.IsFromMockProvider)方法中,它转到CustomMapRendererGetViewForAnnotation并运行var customPin = GetCustomPin(annotation as MKPointAnnotation); method as if it was trying to get the map pins which doesn't make sense to me, which throws a System.Reflection.TargetInvocationException because the CustomPin GetCustomPin(MKPointAnnotation annotation) 's annotation argument is null, of course it's not supposed to call it right?方法好像它试图获取 map 引脚,这对我来说没有意义,这会引发System.Reflection.TargetInvocationException因为CustomPin GetCustomPin(MKPointAnnotation annotation)annotation参数是 null,当然它不应该调用对的?

What's weird is that the call to the method: var customPin = GetCustomPin2(annotation as MKPointAnnotation);奇怪的是对方法的调用: var customPin = GetCustomPin2(annotation as MKPointAnnotation); actually passes the lat and lon as parameters, I'm confused.实际上将 lat 和 lon 作为参数传递,我很困惑。

Should it call it in the first place?它应该首先调用它吗?

Does anyone knows why it does that?有谁知道它为什么这样做? I haven't had this problem on Android and can't check if it does the same on Android at the moment but if anyone knows why it does that and what the solution is please help.我在 Android 上没有遇到这个问题,目前无法检查它是否在 Android 上也有同样的问题,但如果有人知道它为什么会这样以及解决方案是什么,请帮忙。

Thank you谢谢

Does the problem persists if you remove the plugin Xamarin.Essentials Geolocation ?如果您删除插件Xamarin.Essentials Geolocation问题是否仍然存在?

Have you tried on a real device?你在真机上试过吗?


Look at the definition(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.当注释的位置在 map 上可见时调用 GetViewForAnnotation 方法,用于在显示之前自定义注释。

This method should not be called if you don't set Pins on map.如果您没有在 map 上设置Pins ,则不应调用此方法。

Try to add the following code尝试添加以下代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM