地理定位;无法使用iOS PCL Xamarin表单

本文关键字:iOS PCL Xamarin 表单 定位 | 更新日期: 2023-09-27 18:29:16

我正试图借助此示例获取设备位置https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Geolocator.这在模拟器上有效,但在iPadAir上不起作用。我的info.plist文件包含定位所需的所有密钥。,NSLocationUsageDescription和NSLocationWhenUseUsageDescription

代码

if (this.manager.RespondsToSelector(new Selector("requestWhenInUseAuthorization")))
{
    this.manager.RequestWhenInUseAuthorization();
} 

使用Geolocator对象的代码段。。

IGeolocator geolocator = null;
geolocator = DependencyService.Get<IGeolocator>();
if (geolocator != null)
{
    if (geolocator.IsGeolocationEnabled)
    {
        try
        {
            if (!geolocator.IsListening)
                geolocator.StartListening(1000, 1000);
            var task = await geolocator.GetPositionAsync(10000, CancellationToken.None);
            if(task != null)
            {
                this.Position = task;
            }

但是LocationUpdated事件从未启动。。。。。。

设备上的位置已打开。

如果有人有类似的问题。。。。。。。请帮忙。。。。。曾经,这在模拟器上工作。

我在iPad Air上使用VS 2013、Xamarin 3.7、Xamarin.iOS 8.4、iOS 8.1。

地理定位;无法使用iOS PCL Xamarin表单

所以这在iPad空气模拟器中运行良好?这听起来像是您正在使用的设备特有的问题。也许有人在允许位置弹出窗口上点击"取消"?

转到Settings->General->Restrictions->Location Services。确保您的应用程序已打开并允许所有内容。我还会尝试另一个应用程序,并确保该应用程序中的位置正常。

您是否在没有Xlabs地理定位器服务的情况下尝试过,因为上次我尝试时,它有问题

此外,在您的代码中,我没有看到您附加到PositionChanged事件处理程序:

_geolocator.PositionChanged += OnPositionChanged;

如果你想连续定位,这是必要的。

如果不使用XLabs,我会这样做:

  public bool Start(ActivityType activity = ActivityType.Other, bool alsoWhenInBackground = false, LocationAccuracy accuracy = LocationAccuracy.AccurracyBestForNavigation,
                      double minDistanceBetweenUpdatesInMeters = 0, TimeSpan? maxDelayBetweenUpdates = null)
    {
        if (manager != null)
            return true;
        manager = new CLLocationManager();
        manager.Failed += (sender, args) => FireError(args.Error.ToString());
        manager.LocationsUpdated += (sender, args) => FireLocationUpdated(args.Locations);
        //manager.AuthorizationChanged
        manager.ActivityType = (CLActivityType)activity;
        manager.DesiredAccuracy = Accuracies[(int)accuracy];
        if (maxDelayBetweenUpdates != null || minDistanceBetweenUpdatesInMeters > double.Epsilon)
            manager.AllowDeferredLocationUpdatesUntil(minDistanceBetweenUpdatesInMeters, (maxDelayBetweenUpdates ?? TimeSpan.Zero).TotalSeconds);
        if (alsoWhenInBackground)
            manager.PausesLocationUpdatesAutomatically = true;
        //Required: ask for authorization
        if (AuthorizationStatus == AuthorizationStatus.NotDetermined)
            AskAuthorization(alsoWhenInBackground);
        var authStatus = AuthorizationStatus;
        if (authStatus < AuthorizationStatus.AuthorizedAlways && authStatus != AuthorizationStatus.NotDetermined)
        {
            System.Diagnostics.Debug.WriteLine("user denied access to location");
            return false;
        }
        if (authStatus == AuthorizationStatus.AuthorizedWhenInUse && alsoWhenInBackground)
        {
            System.Diagnostics.Debug.WriteLine("alsoWhenInBackground is true, but user denied access to location in background");
            return false;
        }
        manager.StartUpdatingLocation();
        return true;
    }
public bool AskAuthorization(bool alsoWhenInBackground = false)
    {
        if (AuthorizationStatus != AuthorizationStatus.NotDetermined)
            return false;
        if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
        {
            if (alsoWhenInBackground)
                manager.RequestAlwaysAuthorization();
            else
                manager.RequestWhenInUseAuthorization();
        }
        return true;
    }