本文介绍了地理位置无法使用iOS PCL Xamarin表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此示例的帮助下,我正尝试获取设备位置 https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Geolocator .这适用于模拟器,但不适用于iPad Air.我的info.plist文件包含位置所有必需的键.NSLocationUsageDescription和NSLocationWhenInUseUsageDescription

I'm trying to get device location with help of this example https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Geolocator. This works on simulators , but on iPad Air doesn't work. My info.plist file containing all necessary keys for location., NSLocationUsageDescription and NSLocationWhenInUseUsageDescription

以代码

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

使用Geolocator对象的代码段.

piece of code where using Geolocator object..

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事件永远不会触发……

but LocationUpdated event never fire......

设备上的位置已打开.

如果有人遇到类似的问题.......请帮助.....一次,可以在模拟器上进行.

If anyone have similar issue....... please help..... Once, this work on simulators.

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

I use VS 2013, Xamarin 3.7, Xamarin.iOS 8.4 , iOS 8.1 on iPad Air.

推荐答案

您是否尝试了不使用Xlabs geoocator服务的原因,因为上次尝试时,它存在问题.

Did you try without the Xlabs geoocator service, because last time I tried, it was buggy.

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

Beside, in your code I dont see that you attache to the PositionChanged Event handler :

_geolocator.PositionChanged += OnPositionChanged;

如果要连续进行位置更新,则需要此.

This is needed if you want continuous location upadte.

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

Without using XLabs I would do something like this :

  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;
    }

这篇关于地理位置无法使用iOS PCL Xamarin表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 03:50