繁体   English   中英

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

[英]Geolocation doesn't work iOS PCL 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。

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

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

您是否在没有Xlabs geoocator服务的情况下进行了尝试,因为上次尝试时,它存在问题。

另外,在您的代码中,我看不到您附加到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;
    }

暂无
暂无

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

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