Warning: join(): Invalid arguments passed in /home/troyb/troybrant.net/blog/wp-content/themes/hybrid-hacked/hybrid-hacked/library/functions/breadcrumbs.php on line 79

Detecting Bad CoreLocation Data

CoreLocation can (and will) give you poor location data. Over the course of developing RunMonster, I have become painfully aware of this fact. It turns out, though, that you can detect and discard the most egregiously bad location data using a few simple tests.

When a new point comes in, it is invalid and can be discarded if it matches any of the following criteria:

  • The location is nil.
  • The location’s horizontalAccuracy is < 0.
  • The timestamp of the new location is earlier than the timestamp of the previous location, indicating the points came in out of order.
  • The timestamp of the new location is set to a time before your app was even initialized.

In regards to the last bullet, the CoreLocation framework seems to cache and report points from the last time the GPS unit was used. For instance, if you last ran the GPS in Montana and then open your app in Georgia, the first point could be the last cached point from Montana. If you are running a distance-tracking application, then your app would merrily add thousands of miles to your total distance.

The Code

The helper method below checks for these four cases to determine if the newly reported location is valid or not:

- (BOOL)isValidLocation:(CLLocation *)newLocation
    withOldLocation:(CLLocation *)oldLocation
{
    // Filter out nil locations
    if (!newLocation)
    {
        return NO;
    }
    
    // Filter out points by invalid accuracy
    if (newLocation.horizontalAccuracy < 0)
    {
        return NO;
    }
    
    // Filter out points that are out of order
    NSTimeInterval secondsSinceLastPoint =
        [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];
    
    if (secondsSinceLastPoint < 0)
    {
        return NO;
    }
    
    // Filter out points created before the manager was initialized
    NSTimeInterval secondsSinceManagerStarted =
        [newLocation.timestamp timeIntervalSinceDate:locationManagerStartDate];
    
    if (secondsSinceManagerStarted < 0)
    {
        return NO;
    }
    
    // The newLocation is good to use
    return YES;
}

locationManagerStartDate is an NSDate that records when the CLLocationManager is initialized:

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];

locationManagerStartDate = [[NSDate date] retain];

That’s a Wrap

If you are developing any kind of distance-tracking application that uses the phone’s GPS, you should definitely consider using the filters above. These particular error cases were discovered through trial and error, and as Apple improves CoreLocation, they may very well fix these problems. However, these filters have helped RunMonster avoid the wacky GPS issues that plague many distance-tracking applications.

13 Responses to “Detecting Bad CoreLocation Data”

  1. Hi Troy,

    as always, another great blog post! But are you sure that points, that are not in order should be filtered out? The iPhone Application Programming Guide says in chapter 8 “Getting the User’s Current Location”:

    “Note: The Core Location framework records timestamp values at the beginning of each location query, not when that query returns. Because Core Location uses several different techniques to get a location fix, queries can sometimes come back in a different order than their timestamps might otherwise indicate. As a result, it is normal for new events to sometimes have timestamps that are slightly older than those from previous events. The framework concentrates on improving the accuracy of the location data with each new event it delivers, regardless of the timestamp values.”

    Greetings from Germany,
    Manuel

  2. You have tested it and writing form your personal experience or you find some information online?

  3. nice post. thanks.

  4. I think I can take this information one step further. You should not just ignore but blacklist any points that appear out of order, and then drop any further references to that exact location: that seems clear out the repeated jumps to a specific point I see quite often.

  5. Hey Troy,
    RunMonster, app looks awesome congrats.
    I’m working on some app which is working wich is needed turn by turn correct gps data. Let me ask you a question, what is the runmonster’s CLLocationManager intialize values ?
    .desiredAccuracy = ?
    .distanceFilter = ?

    or what is your suggestion ?

    Thanks

  6. Hi,

    Nice tips, and a nice app you got there in appstore, I was wondering what methods you used to draw the routes over the map? Can you try to write a blogpost about it aswell?

    Thanks ;-)

  7. Dude, you really have to check your Comments more often! In case you missed my comment, here’s the lowdown.In recent months, this crazy dude has been doing the exact OPPOSITE of what everybody else in “Internet Marketing” has beendoing, and it’s proven to be a more profitable approach than anything I’ve ever seen before , watch this video BUZZBOOZ.COM The good part starts somewhere around the 2 minute mark.

  8. Hello there! This blog post couldn’t be written much better! Looking at this post reminds me of my previous roommate! He continually kept preaching about this. I am going to forward this information to him. Pretty sure he’ll have a great read.
    Thanks for sharing!

  9. I was more than happy to find this great site. I wanted to thank you for ones time due
    to this fantastic read!! I definitely really liked every little bit of it and i also
    have you saved as a favorite to check out new things on your blog.

  10. I am really impressed with your writing abilities and also with the
    layout to your blog. Is this a paid subject or did you customize
    it yourself? Anyway keep up the nice quality writing,
    it is rare to see a nice weblog like this one these days..

  11. Simply wish to say your article is as astonishing.

    The clarity on your submit is just nice and i could suppose you’re an expert in
    this subject. Fine along with your permission let me
    to seize your feed to stay updated with imminent post.
    Thank you 1,000,000 and please carry on the gratifying work.

  12. Do you mind if I quote a few of your articles
    as long as I provide credit and sources back to your website?

    My blog site is in the exact same niche as yours and my visitors would definitely benefit from a lot of the information you present here.
    Please let me know if this okay with you. Regards!

  13. Hiya! I know this is kinda off topic nevertheless I’d figured I’d ask.
    Would you be interested in exchanging links or maybe guest
    authoring a blog article or vice-versa? My site discusses a
    lot of the same topics as yours and I believe we could greatly benefit from each
    other. If you happen to be interested feel free to send me an e-mail.

    I look forward to hearing from you! Wonderful blog by the way!

Leave a Reply