In Xcode 4.5 apple introduced apple new maps. My application heavliy requires map services. And I have noticed in my application it shows the wrong current location until yo
you should check the timestamp .. if i understand your app logic correctly, you could do something like -
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSDate* time = newLocation.timestamp;
NSTimeInterval timePeriod = [time timeIntervalSinceNow];
if(timePeriod < SOME_MINIMUM_TIME_PERIOD ) { //for eg: timePeriod < 1.0
totalDistance += [newLocation distanceFromLocation:oldLocation];
} else {
// skip.. or do what you do on a 'location-not-updated' event
}
}
I have noticed the same problem with Xcode 4.4+. The problem only occurs (randomly, or so it seems) within Xcode: if you upload the app to the App Store, this is not a problem anymore. In the meantime, please file a bug.
The old approach from apple docs seems still working in iOS6 (didn't notice this in my active app (it tracks user's route via gps))
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return;
if (newLocation.horizontalAccuracy < 0) return;
// proceed with coords here
}
UPDATE from the discussion: Calculating total and current distance could be done like this (excluding some minor stuff):
// somewhere at the top
CLLocation* lastUsedLocation = nil; // last location used in calculation
CLLocation* pointA = nil; // start of the track
double totalDistance = 0; // total distance of track
double currentDistance = 0; // distance between startTrack point and current location
...
// when you start updating location:
- (void) startTracking {
lastUsedLocation = nil;
pointA = nil;
totalDistance = 0;
currentDistance = 0;
[locationManager startUpdatingLocation];
}
...
// location update callback
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return; // filter cached
if (newLocation.horizontalAccuracy < 0) return; // filter invalid
if(!pointA) pointA = [newLocation retain];
if(lastUsedLocation)
{
totalDistance += [newLocation distanceFromLocation:lastUsedLocation];
}
currentDistance = [pointA distanceFromLocation:newLocation];
[lastUsedLocation release];
lastUsedLocation = [newLocation retain];
}
If you need the option to turn off background location on purpose you disable it manually like:
- (void)applicationDidEnterBackground:(UIApplication *)application {
if(backgroundLocationDisabled)
{
[locationManager stopUpdatingLocation];
// additional stuff
}
}