CLLocationManager didUpdateToLocation checking failing

纵然是瞬间 提交于 2019-12-25 01:37:31

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/6980572/cllocationmanager-didupdatetolocation-checking-failing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!