CLLocationManager didUpdateToLocation 检查失败
我正在尝试使用 CLLocationManager 获取位置坐标。这是我的代码
- (void)viewDidLoad {
[super viewDidLoad];
//instantiate location manager and set delegate
self.locationManager=[[CLLocationManager alloc]init];
locationManager.delegate=self;
// can be set to 100m,1km,3km etc.
//locationManager.distanceFilter=10.0f;
locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;
//start updating the delegate
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
// Check if the coordinates are different from the old ones
if (newLocation.coordinate.latitude != oldLocation.coordinate.latitude &&
newLocation.coordinate.longitude != oldLocation.coordinate.longitude) {
NSLog(@"not equal");
} else {
NSLog(@"equal");
}
}
,但是我发现该条件被调用了两次。第一次满足条件并打印不等于,并立即再次调用并打印“等于”。有人可以帮我吗?我做错了什么?
谢谢
I am trying to get the location coordinates using CLLocationManager. Here is my code
- (void)viewDidLoad {
[super viewDidLoad];
//instantiate location manager and set delegate
self.locationManager=[[CLLocationManager alloc]init];
locationManager.delegate=self;
// can be set to 100m,1km,3km etc.
//locationManager.distanceFilter=10.0f;
locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;
//start updating the delegate
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
// Check if the coordinates are different from the old ones
if (newLocation.coordinate.latitude != oldLocation.coordinate.latitude &&
newLocation.coordinate.longitude != oldLocation.coordinate.longitude) {
NSLog(@"not equal");
} else {
NSLog(@"equal");
}
}
However i find the condition is called twice. First time the condition is satisfied and prints not equal and immediately its called again and prints "equal". Can some1 help me out ? What am i doing wrong ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为 CoreLocation 会缓存您的最后位置,并在您调用 startUpdatingLocation 后立即返回它,因此您必须验证坐标时间戳,它太旧了,您可以忽略旧坐标。
更新:
createLocatinoImprovementTimer 方法用于启动计时器,该计时器将工作一定的时间,如果没有停止,它将发送并更新最后一次的位置通知。该计时器将有助于在通知控制器之前等待更准确的协调。
This is because CoreLocation cache your last location and return it immediately after you called startUpdatingLocation so you have to validate coordinate timestamp and it is too old, you can ignore old coordinate.
UPDATE:
createLocatinoImprovementTimer method used to launch timer, which will work for certain amount of time and if it wasn't stopped it will send and update notification with location, which was last. This timer will help to wait for coordinate with better accuracy before notify controllers.