CLLocationManager didUpdateToLocation检查失败 [英] CLLocationManager didUpdateToLocation checking failing

查看:64
本文介绍了CLLocationManager didUpdateToLocation检查失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用CLLocationManager获取位置坐标.这是我的代码

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 ?

谢谢

推荐答案

这是因为CoreLocation会缓存您的最后一个位置,并在您调用 startUpdatingLocation 后立即返回它,因此您必须验证坐标时间戳,它是太旧了,您可以忽略旧的坐标.

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.

更新:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    if([newLocation horizontalAccuracy] < 0.0f) return;
    if(fabs([[newLocation timestamp] timeIntervalSinceNow]) > kCLLocationMaximumLocationDataAge) return;
    if(fabs([[oldLocation timestamp] timeIntervalSinceNow]) < kCLLocationMaximumLocationDataAge && [newLocation getDistanceFrom:oldLocation] < 0.1f && [newLocation horizontalAccuracy] == [oldLocation horizontalAccuracy])
        return;
    if(((runningHighPreciseLocationDetectionService||runningLowPowerLocationDetectionService) && ([newLocation horizontalAccuracy] <= kCLLocationAccuracyHundredMeters))){
        NSLog(@"---> \n%@\n%@\nHorizontal accurecy: %f\nLocation age: %fs\nOld location age: %fs", NSStringFromSelector(_cmd), newLocation, newLocation.horizontalAccuracy, fabs([[newLocation timestamp] timeIntervalSinceNow]), fabs([[oldLocation timestamp] timeIntervalSinceNow]));
    } else {
        NSLog(@"\n-------------- BAD ---------------\n%@\n%@\nHorizontal accurecy: %f\nLocation age: %fs\nOld location age: %fs\n----------------------------------", NSStringFromSelector(_cmd), newLocation, newLocation.horizontalAccuracy, fabs([[newLocation timestamp] timeIntervalSinceNow]), fabs([[oldLocation timestamp] timeIntervalSinceNow]));
    }
    if(((runningHighPreciseLocationDetectionService||runningLowPowerLocationDetectionService) && ([newLocation horizontalAccuracy] <= kCLLocationAccuracyHundredMeters))){
        [self stopLocationImprovementTimer];
    } else [self createLocatinoImprovementTimer];
}

createLocatinoImprovementTimer 方法用于启动计时器,该方法将在一定时间内起作用,如果未停止,它将发送并更新带有位置的通知,最后一次.此计时器将有助于在通知控制器之前以更高的精度等待协调.

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.

这篇关于CLLocationManager didUpdateToLocation检查失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆