InsaneOstrich
InsaneOstrich

Reputation: 127

Distance to a location while user is in motion

I'm in the process of writing an application that shows the user's distance from a fixed point as the user walks around (i.e. the label showing the distance from the user to the point is updated every time the user moves). I use a CLLocationManager with the code shown below:

- (void)viewDidLoad
{
    locationManager=[[CLLocationManager alloc]init]; 
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    [locationManager startUpdatingLocation];      
}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation      *)newLocation fromLocation:(CLLocation *)oldLocation 
{   
    CLLocationDistance meters = [newLocation distanceFromLocation:fixedPoint];
    self.distanceLabel.text = [[NSString alloc] initWithFormat:@"Distance: %.1f feet", meters*3.2808399];
}

The label that is supposed to show the distance from the user to the point isn't updated constantly and when it is updated, it doesn't usually show the correct distance from the user to the fixed point. I was wondering if there is a better way for me to try and do this, or do the fundamental limitations of the core location framework make this impossible. Any help will be greatly appreciated.

Upvotes: 3

Views: 1180

Answers (1)

progrmr
progrmr

Reputation: 77271

Are you filtering out old (cached) positions? You should also filter based on accuracy, you probably don't want low accuracy locations.

You won't get continous or periodic update, the callback only occurs when the location has changed.

Assuming the device has GPS and can see enough GPS satellites to get a good position, this works fine.

-(void)locationManager:(CLLocationManager *)manager 
   didUpdateToLocation:(CLLocation *)newLocation 
          fromLocation:(CLLocation *)oldLocation {

    NSTimeInterval age = -[newLocation.timestamp timeIntervalSinceNow]; 

    if (age > 120) return;    // ignore old (cached) updates

    if (newLocation.horizontalAccuracy < 0) return;   // ignore invalid udpates

    ...
}

Upvotes: 4

Related Questions