当 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.
但是,在实践中,CLLocationManager
的location
属性不会更新。为什么不呢?
我创建了一个示例项目来演示这一点: 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 上找到错误报告的镜像:
关于ios - 为什么发送 CLLocationManagerDelegate 消息后 [CLLocationManager location] 没有更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14759599/