ios - 为什么发送 CLLocationManagerDelegate 消息后 [CLLocationManager location] 没有更新?

标签 ios core-location cllocationmanager

locationManager:didUpdateLocations:(或其弃用的等效 locationManager:didUpdateToLocation:fromLocation:)消息发送到 CLLocationManagerDelegate 时, CLLocationManagerDelegate Protocol Reference指出:

By the time this message is delivered to your delegate, the new location data is also available directly from the CLLocationManager object. The newLocation parameter may contain the data that was cached from a previous usage of the location service. You can use the timestamp property of the location object to determine how recent the location data is.

但是,在实践中,CLLocationManagerlocation 属性不会更新。为什么不呢?

我创建了一个示例项目来演示这一点: https://github.com/sibljon/CoreLocationDidUpdateToLocationBug

相关代码在JSViewController中,摘录如下:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    self.locationManager.distanceFilter = 10000.0; // 10 km
    self.locationManager.delegate = self;
    self.locationManager.purpose = @"To show you nearby hotels.";
    [self.locationManager startUpdatingLocation];

    [self.locationManager startUpdatingLocation];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(appWillEnterForeground:)
                                                 name:UIApplicationWillEnterForegroundNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(appDidEnterBackground:)
                                                 name:UIApplicationDidEnterBackgroundNotification
                                               object:nil];
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"New location: %@", newLocation);
    NSLog(@"Old location: %@", oldLocation);
    NSLog(@"- [CLLocationManager location]: %@", manager.location);
}

//- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
//{
//    for (CLLocation *location in locations)
//    {
//        NSLog(@"Current location: %@", locations);
//    }
//    NSLog(@"- [CLLocationManager location]: %@", manager.location);
//}

#pragma mark - Notifications

- (void)appWillEnterForeground:(NSNotification *)notification
{
    [self.locationManager startUpdatingLocation];
}

- (void)appDidEnterBackground:(NSNotification *)notification
{
    [self.locationManager stopUpdatingLocation];
}

最佳答案

我认为这是一个错误,我已向 Apple 提交错误报告。可以在 Open Radar 上找到错误报告的镜像:

http://openradar.appspot.com/radar?id=2682402

关于ios - 为什么发送 CLLocationManagerDelegate 消息后 [CLLocationManager location] 没有更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14759599/

相关文章:

objective-c - 以半径搜索位置(cocoa-touch)

ios - 使用 EXC_BAD_INSTRUCTION 在 CLLocationManager.authorizationStatus() 行上 Swift 崩溃

ios8 - 如何在IOS SDK 8.0中获取当前位置的经纬度

ios - 当触摸位于不同的 subview 中时, super View 上的 UIPinchGestureRecognizer 不会尝试识别捏合

ios - Opengl ES - 绘制多个对象的最佳方法是什么

ios - 如何调整 4inch theos-application

ios - TableView (:cellForRowAtIndexPath:) Not Called

objective-c - CoreLocation 停止为几个应用程序用户工作

ios - distanceFromLocation - 计算两点之间的距离

objective-c - iOS 8 Mapkit Objc 无法获取用户位置