地理位置不适用于iOS PCL Xamarin表单

时间:2014-11-04 15:59:47

标签: c# xamarin.ios xamarin xamarin.forms

我试图通过此示例https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Geolocator获取设备位置。这适用于模拟器,但在iPad Air上不起作用。我的info.plist文件包含所有必要的位置密钥。,NSLocationUsageDescription和NSLocationWhenInUseUsageDescription

代码

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。

2 个答案:

答案 0 :(得分:1)

所以这在iPad空气模拟器中工作正常吗?这听起来像是您正在使用的设备特有的问题。也许有人在允许位置弹出窗口中点击“取消”?

转到Settings->General->Restrictions->Location Services。确保所有内容都已打开并允许您的应用。我还会尝试其他应用,并确保该位置适用于该应用。

答案 1 :(得分:0)

你是否尝试过没有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;
    }