【问题标题】:CLLocationManager does not send location to the didUpdateLocations methodCLLocationManager 不会将位置发送到 didUpdateLocations 方法
【发布时间】:2013-03-27 10:41:54
【问题描述】:

我正在开发一个启用了ARCiOS 6.1 iPhone app,它将使用CLLocationManager 来跟踪设备位置。

更新

我有一个DestinationViewController,它实现了CLLocationManagerDelegate。它创建CLLocationManager 并进行设置。我已将CoreLocation framework 导入到我的项目中。

我尝试了很多调试,可以看到 CLLocationManager 已创建并且没有错误出现。但问题是[CLLocationManager authorizationStatus]kCLAuthorizationStatusNotDetermined[self.locationManager startUpdatingLocation]; 之前和之后。 所以没有提示询问是否允许使用此应用的位置服务。因为这个方法永远不会被触发。

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:  
(NSArray *)locations

我还尝试在网上搜索几个小时以寻找类似的示例,但没有运气。来自 DestinationViewController 的代码发布在下面。

界面

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface DestinationViewController : UIViewController <CLLocationManagerDelegate>

实施

#import "DestinationViewController.h"

@interface DestinationViewController ()
@property (strong, nonatomic) CLLocationManager *locationManager;
@end


@implementation DestinationViewController

// Initializer
- (CLLocationManager *)locationManager
{
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc] init];
    }
    return _locationManager;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self startLocation];
}


- (void)startLocation
{
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.distanceFilter = 1;

    NSString *error;
    if (![CLLocationManager locationServicesEnabled]) {
        error = @"Error message";
    }

    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (status == kCLAuthorizationStatusRestricted ||
        status == kCLAuthorizationStatusDenied ||) {
        error = @"Error message";
    }

    if (error) {
        NSLog(error);
    }
    else
    {

        status = [CLLocationManager authorizationStatus];

    self.locationManager.pausesLocationUpdatesAutomatically = NO;

        [self.locationManager startUpdatingLocation];

        status = [CLLocationManager authorizationStatus];

        NSLog(@"CLLocationManager is %@", self.locationManager);
        NSLog(@"Location is %@", self.locationManager.location);

        [self updateUI];
    }
}


- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations
{
    CLLocation *recentLocation = [locations lastObject];
    NSLog(@"Found location");
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError");
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    NSLog(@"Change in authorization status");
}

【问题讨论】:

  • 这是在模拟器还是设备上?如果是模拟器,您是否允许 XCode 模拟该位置?
  • 两种都试过了。我创建了一个自定义位置并尝试进行高速公路行驶
  • 您可以尝试实现已弃用的locationManager:didUpdateToLocation:fromLocation: 并查看它是否被调用?
  • 我也试过了。实际上,当我尝试运行 Apple Maps 应用程序时,现在我的模拟器显示“无法确定位置”。如果我在设置中查看,则允许应用获取位置。
  • 您是否检查并查看您在“didFailWithError”中遇到了什么样的错误?因为这将有助于确定您的方法未触发的原因。

标签: iphone ios objective-c core-location cllocationmanager


【解决方案1】:

您的代码应该可以工作。

确保您的应用程序被授权使用位置服务。

您可以使用以下方法检查代码中的授权状态

 + (CLAuthorizationStatus)authorizationStatus

编辑

kCLAuthorizationStatusNotDetermined 很可能是由位置服务被禁用引起的。您应该首先检查位置服务状态。代码应该是这样的

if([CLLocationManager locationServicesEnabled]) {
    // Location Services Are Enabled
    switch([CLLocationManager authorizationStatus]) {
        case kCLAuthorizationStatusNotDetermined:
            // User has not yet made a choice with regards to this application
            break;
        case kCLAuthorizationStatusRestricted:
            // This application is not authorized to use location services.  Due
            // to active restrictions on location services, the user cannot change
            // this status, and may not have personally denied authorization
            break;
        case kCLAuthorizationStatusDenied:
            // User has explicitly denied authorization for this application, or
            // location services are disabled in Settings
            break;
        case kCLAuthorizationStatusAuthorized:
            // User has authorized this application to use location services
            break;
    }
} else {
    // Location Services Disabled
}

【讨论】:

  • 感谢您的回答。我做了以下事情: CLAuthorizationStatus status = [CLLocationManager authorizationStatus];在调试器中我可以看到它是 kCLAuthorizationStatusAuthorized。所以它应该可以工作,但该方法仍然没有被调用。
  • 经过大量调试后,我可以看到,当我在 iPhone 5 上运行应用程序时,它的 CLAuthorizationStatus 在调用 startUpdatingLocation 之前和之后都设置为 kCLAuthorizationStatusNotDetermined。为什么没有向用户显示警报?
  • kCLAuthorizationStatusNotDetermined 的苹果文档指出“用户尚未选择此应用程序是否可以使用位置服务。”那么它可以t be disabled. I can´t find my app in the Privacy Settings in Location Services. Do you know why it doesnt 出现在那里吗?
  • kCLAuthorizationStatusNotDetermined 表示用户尚未做出选择,但我看到有消息称,当为应用程序启用位置服务但未打开设备时,可能会发生这种情况。您确定在设置->隐私->设备上的定位服务下启用了定位服务吗?
  • 是的,它已开启。我正在我的 iPhone 5 上进行测试,它可以很容易地在地图应用中找到它的位置。但我的应用不在使用定位服务的应用列表中?
【解决方案2】:

我终于解决了!我的代表设置不正确。 而不是这样做:

self.locationManager.delegate = self;

我改成:

[self.locationManager setDelegate:self];

现在我的委托方法被触发了。

感谢帮助的人!

【讨论】:

  • 我从来没有遇到过这样的问题:/
  • 我(也)怀疑这与它有什么关系。
猜你喜欢
  • 1970-01-01
  • 2017-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多