CLLocationManager didUpdateToLocation 检查失败

发布于 2024-11-28 10:28:58 字数 1066 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

属性 2024-12-05 10:28:58

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

更新:

- (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 方法用于启动计时器,该计时器将工作一定的时间,如果没有停止,它将发送并更新最后一次的位置通知。该计时器将有助于在通知控制器之前等待更准确的协调。

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:

- (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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文