簡體   English   中英

CLLocationManager不調用didUpdateLocations

[英]CLLocationManager not calling didUpdateLocations

我一直無法弄清楚為什么位置管理器不向我提供更新。 我添加了密鑰,該密鑰已在模擬器和設備上進行了測試。

編輯: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

也許這只是拼寫錯誤的結果。

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

應該

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM