Distance to a location while user is in motion
Asked Answered
M

1

3

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.

Martynne answered 23/11, 2011 at 19:15 Comment(5)
I would write these Locations (newLocation and oldLocation) to a log so that I could inspect the values. Is there a chance that these values are not quite fine enough to get a good measurement in feet? Also, according to Apple, iOS devices first connect to WiFi, followed by Cell Towers, followed by GPS sattelites when determining a user's location. This is because WiFi connects faster than Cell Towers, as well as Cell Towers to GPS sats. The device can be more responsive if it connects to the devices in that order. That could be part of your problem.Albers
Is that your actual code? I don't see where the locationManager's delegate is set (without which the delegate method won't get called). How is fixedPoint set?Tornado
Yeah, I set the delegate and fixedPoint is initialized with arbitrary coordinates for testing purposes. I tried what RLH suggested and the points captured in the log are spread out and not accurate enough for my purposes. I'm getting the impression that what I'm trying to do might be beyond the limitations of the mechanisms used by the phone to determine its current location.Martynne
By the way, the "best" accuracy setting is actually kCLLocationAccuracyBestForNavigation though it will consume the most power.Tornado
kCLLocationAccuracyBestForNavigation only works while plugged into a power source. On a side note, use NSString stringWithFormat instead, that is a (small) memory leak there.Speculator
B
4

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

    ...
}
Blanding answered 23/11, 2011 at 23:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.