调用函数“didUpdateToLocation”而不进行更改
Posted
技术标签:
【中文标题】调用函数“didUpdateToLocation”而不进行更改【英文标题】:Function "didUpdateToLocation" being called without changes 【发布时间】:2012-09-28 04:03:55 【问题描述】:我这样初始化 locationManager:
if (!self.locManager)
self.locManager = [[CLLocationManager alloc] init];
self.locManager.delegate = self;
[locManager startMonitoringSignificantLocationChanges];
我的设备没有移动,并且每次都在调用“didUpdateToLocation”。 可能是什么问题? 谢谢
【问题讨论】:
【参考方案1】:didUpdateToLocation
可能会因多种原因而更新,处理此问题的一个好策略是根据时间戳逐步过滤结果,然后根据要求的准确性。
苹果在LocateMe sample app中提供了一个很好的例子:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
// test the age of the location measurement to determine if the measurement is cached
// in most cases you will not want to rely on cached measurements
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return;
// test that the horizontal accuracy does not indicate an invalid measurement
if (newLocation.horizontalAccuracy < 0) return;
// test the measurement to see if it is more accurate than the previous measurement
if (self.bestEffortAtLocation == nil || self.bestEffortAtLocation.horizontalAccuracy > newLocation.horizontalAccuracy)
// store the location as the "best effort"
self.bestEffortAtLocation = newLocation;
// test the measurement to see if it meets the desired accuracy
//
// IMPORTANT!!! kCLLocationAccuracyBest should not be used for comparison with location coordinate or altitidue
// accuracy because it is a negative value. Instead, compare against some predetermined "real" measure of
// acceptable accuracy, or depend on the timeout to stop updating. This sample depends on the timeout.
//
if (newLocation.horizontalAccuracy <= locationManager.desiredAccuracy)
// we have a measurement that meets our requirements, so we can stop updating the location
//
// IMPORTANT!!! Minimize power usage by stopping the location manager as soon as possible.
//
[self stopUpdatingLocation:NSLocalizedString(@"Acquired Location", @"Acquired Location")];
【讨论】:
谢谢,但如果我希望定位服务保持打开状态怎么办? stopUpdating 可能会停止位置管理器吗? 此代码允许位置管理器根据需要经常更新,以将用户定位到所需的准确性。此时,电源管理停止更新是礼貌和明智的。如果您的应用需要在用户更改位置时对其进行监控,那么您可以注册重大更改:[locationManager startMonitoringSignificantLocationChanges];
请参阅docs【参考方案2】:
您是否检查位置差异? CoreLocation 也会在精度、航向或速度等其他属性发生变化时调用回调
startMonitoringSignificantLocationChanges
应该给你一个初始修复,然后你会收到“重大变化”的回调(蜂窝塔变化等)
【讨论】:
对不起,我没有完全理解。以上是关于调用函数“didUpdateToLocation”而不进行更改的主要内容,如果未能解决你的问题,请参考以下文章
CLLocationManager 委托方法 didUpdateToLocation 没有被调用
didUpdateToLocation 调用了两次,好的。为啥 oldLocation 两次都为零?
一段时间后没有调用 CLLocationManager didUpdateToLocation