I am starting to talk with CLLocationManager and I am trying to use the callback method below. I went outside to check it on my iPhone, and from the first few updates I seem to get gibberish, because suddenly my distance is 39 meters, although I didn’t move anywhere. Or sometimes it starts at 5, and then jump 20 meters, again without me moving somewhere. I went outside and went, and the updates from the starting point seemed “good,” and then when I returned, I returned to the starting point of 39 meters. I was wondering if I am doing the below correctly. I also included the viewDidLoad method, where I initialize my CLLocationManager object.
Is there any way to make sure my first values are accurate? Thanks in advance.
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (startingPoint == nil) {
NSLog(@"NIL starting point");
self.startingPoint = newLocation;
}
CLLocationDistance distance = [newLocation distanceFromLocation:startingPoint];
NSString *distanceString = [[NSString alloc] initWithFormat:@"%g m", distance];
distanceLabel.text = distanceString;
[distanceString release];
numberOfUpdates++;
NSString *countString = [[NSString alloc] initWithFormat:@"%d", numberOfUpdates];
countLabel.text = countString;
[countString release];
}
source
share