public
Created

Is it a valid CoreLocation GPS CLLocaiton point?

  • Download Gist
IsValidLocation.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
static int distance_offset_in_meters = 800;
 
- (bool)_isValidNewLocation:(CLLocation *)newLocation oldLocation:(CLLocation *)oldLocation;
{
if (!newLocation) return false;
if (newLocation.horizontalAccuracy < 0) return false;
NSTimeInterval secondsSinceLastPoint = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];
if (secondsSinceLastPoint < 0) return false;
NSTimeInterval secondsSinceManagerStarted = [newLocation.timestamp timeIntervalSinceDate:self.locationManagerStartDate];
if (secondsSinceManagerStarted < 0) return false;
 
if(newLocation.horizontalAccuracy > distance_offset_in_meters) return false;
NSDate* eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) > 15.0) return false;
 
if (oldLocation.speed < 0)
return false;
 
if (newLocation.speed < 0)
return false;
 
// not executed by iPad / iPod touch
if([[UIDevice currentDevice].model isEqualToString:@"iPhone"])
{
if (!oldLocation.verticalAccuracy || !newLocation.verticalAccuracy)
return false;
if (oldLocation.verticalAccuracy < 0 || newLocation.verticalAccuracy < 0)
return false;
}
 
return true;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.