简体   繁体   English

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

[英]Geolocation doesn't work iOS PCL Xamarin forms

I'm trying to get device location with help of this example https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Geolocator . 我正在尝试借助此示例https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Geolocator来获取设备位置。 This works on simulators , but on iPad Air doesn't work. 这适用于模拟器,但不适用于iPad Air。 My info.plist file containing all necessary keys for location., NSLocationUsageDescription and NSLocationWhenInUseUsageDescription 我的info.plist文件包含位置所有必需的键,NSLocationUsageDescription和NSLocationWhenInUseUsageDescription

in code 在代码中

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

piece of code where using Geolocator object.. 使用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;
            }

but LocationUpdated event never fire...... 但是LocationUpdated事件永远不会触发……

Location on device is on. 设备上的位置已开启。

If anyone have similar issue....... please help..... Once, this work on simulators. 如果有人有类似的问题.......请帮助.....一次,这项工作在模拟器上进行。

I use VS 2013, Xamarin 3.7, Xamarin.iOS 8.4 , iOS 8.1 on iPad Air. 我在iPad Air上使用VS 2013,Xamarin 3.7,Xamarin.iOS 8.4,iOS 8.1。

So this works fine in the iPad air simulator? 所以这在iPad空气模拟器中可以正常工作吗? It sounds like an issue specific to the device you are using. 听起来像是您所使用设备的特定问题。 Maybe someone hit "Cancel" on the allow location popup? 也许有人在允许位置弹出窗口上点击了“取消”?

Go to Settings->General->Restrictions->Location Services . 转到Settings->General->Restrictions->Location Services Make sure everything is turned on and allowed for your app. 确保所有功能均已打开并允许您的应用程序使用。 I would also try another app, and make sure location is working in that app. 我还会尝试另一个应用程序,并确保该应用程序中的位置正在运行。

Did you try without the Xlabs geoocator service, because last time I tried, it was buggy. 您是否在没有Xlabs geoocator服务的情况下进行了尝试,因为上次尝试时,它存在问题。

Beside, in your code I dont see that you attache to the PositionChanged Event handler : 另外,在您的代码中,我看不到您附加到PositionChanged事件处理程序:

_geolocator.PositionChanged += OnPositionChanged;

This is needed if you want continuous location upadte. 如果要连续进行位置调整,则需要此方法。

Without using XLabs I would do something like this : 如果不使用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