CLLocationManager不调用didUpdateLocations

时间:2014-11-29 03:48:08

标签: ios objective-c ios8 cllocationmanager

我一直无法弄清楚为什么位置管理员没有向我提供更新。我已经添加了密钥,在模拟器和设备上进行了测试。

编辑:STDOUT还包含“委托:”

STDOUT

2014-11-28 22:18:30.066 Speedo[14091:150298] Status: 4
2014-11-28 22:18:30.069 Speedo[14091:150298] Is when in use? 1
2014-11-28 22:18:30.158 Speedo[14091:150298] New status: 4
2014-11-28 22:18:30.159 Speedo[14091:150298] Is when in use? 1

FirstViewController.h

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

@interface FirstViewController : UIViewController <CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UILabel* speedLabel;
@end

FirstViewController.cpp

#import "FirstViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface FirstViewController ()

@end

@implementation FirstViewController {
    CLLocationManager *locationManager;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    self.speedLabel.text = @"";

    if([locationManager respondsToSelector: @selector(requestWhenInUseAuthorization)]) {
        [locationManager requestWhenInUseAuthorization];
    }

    // locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    // locationManager.distanceFilter = 0.01;
    [locationManager startUpdatingLocation];

    NSLog(@"Delegate: %@", locationManager.delegate);
    NSLog(@"Status: %d", [CLLocationManager authorizationStatus]);
    NSLog(@"Is when in use? %d", [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

-(void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    NSLog(@"New status: %d", status);
    NSLog(@"Is when in use? %d", [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse);
    [manager startUpdatingLocation];
}

-(void)locationManage: (CLLocationManager *)manager didUpdateLocations:(NSArray*)locations {
    CLLocation* last = [locations lastObject];
    NSLog(@"Speed: %f m/s", last.speed); // meter per second
    // TODO: support mutli-units
    // m/s -> mph
    // 1 -> 2.236
    self.speedLabel.text = [NSString stringWithFormat:@"%d", (int)(last.speed * 2.23694)];
}

@end

1 个答案:

答案 0 :(得分:2)

也许这只是一个错字的结果。

-(void)locationManage: (CLLocationManager *)manager didUpdateLocations:(NSArray*)locations {

应该是

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

您忘记了“locationManager”末尾的“r”。