Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy isn't the [CLLocationManager location] updated when the CLLocationManagerDelegate message is sent?
    text
    copied!<p>When the <code>locationManager:didUpdateLocations:</code> (or its deprecated equivalent <code>locationManager:didUpdateToLocation:fromLocation:</code>) message is sent to the <code>CLLocationManagerDelegate</code>, the <a href="https://developer.apple.com/library/mac/documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html#//apple_ref/occ/intfm/CLLocationManagerDelegate/locationManager%3adidUpdateToLocation%3afromLocation%3a">CLLocationManagerDelegate Protocol Reference</a> states that:</p> <blockquote> <p>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.</p> </blockquote> <p>However, in practice, <code>CLLocationManager</code>'s <code>location</code> property is not updated. Why not?</p> <p>I have created a sample project to demonstrate this: <a href="https://github.com/sibljon/CoreLocationDidUpdateToLocationBug">https://github.com/sibljon/CoreLocationDidUpdateToLocationBug</a></p> <p>The relevant code is in <code>JSViewController</code>, a snippet of which is below:</p> <pre><code>- (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]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload