Sto provando a creare un'app iOS che visualizzi la distanza totale percorsa durante la corsa o la marcia. Ho letto e riletto tutta la documentazione che riesco a trovare, ma ho difficoltà a trovare qualcosa che mi dia una distanza totale precisa.Problemi nel calcolare la distanza totale di camminata/corsa totale con CLLocationManager
Rispetto a Nike + GPS o RunKeeper, la mia app segnala costantemente una distanza più breve. In un primo momento, riferiranno lo stesso, ma man mano che procedo, i valori della mia app e di altre app in esecuzione vanno alla deriva.
Ad esempio, se percorro3,3 chilometri (verificati dal contachilometri della mia auto), Nike + GPS e RunKeeper segnalano ogni volta ~ .3 chilometri, ma la mia app riporterà ~ 0,1 chilometri. newLocation.horizontalAccuracy è costantemente 5.0 o 10.0.
Ecco il codice che sto utilizzando. Mi manca qualcosa di ovvio? Qualche idea su come potrei migliorare questo per ottenere una lettura più accurata?
#define kDistanceCalculationInterval 10 // the interval (seconds) at which we calculate the user's distance
#define kNumLocationHistoriesToKeep 5 // the number of locations to store in history so that we can look back at them and determine which is most accurate
#define kValidLocationHistoryDeltaInterval 3 // the maximum valid age in seconds of a location stored in the location history
#define kMinLocationsNeededToUpdateDistance 3 // the number of locations needed in history before we will even update the current distance
#define kRequiredHorizontalAccuracy 40.0f // the required accuracy in meters for a location. anything above this number will be discarded
- (id)init {
if ((self = [super init])) {
if ([CLLocationManager locationServicesEnabled]) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
self.locationManager.distanceFilter = 5; // specified in meters
}
self.locationHistory = [NSMutableArray arrayWithCapacity:kNumLocationHistoriesToKeep];
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
// since the oldLocation might be from some previous use of core location, we need to make sure we're getting data from this run
if (oldLocation == nil) return;
BOOL isStaleLocation = [oldLocation.timestamp compare:self.startTimestamp] == NSOrderedAscending;
[self.delegate locationManagerDebugText:[NSString stringWithFormat:@"accuracy: %.2f", newLocation.horizontalAccuracy]];
if (!isStaleLocation && newLocation.horizontalAccuracy >= 0.0f && newLocation.horizontalAccuracy < kRequiredHorizontalAccuracy) {
[self.locationHistory addObject:newLocation];
if ([self.locationHistory count] > kNumLocationHistoriesToKeep) {
[self.locationHistory removeObjectAtIndex:0];
}
BOOL canUpdateDistance = NO;
if ([self.locationHistory count] >= kMinLocationsNeededToUpdateDistance) {
canUpdateDistance = YES;
}
if ([NSDate timeIntervalSinceReferenceDate] - self.lastDistanceCalculation > kDistanceCalculationInterval) {
self.lastDistanceCalculation = [NSDate timeIntervalSinceReferenceDate];
CLLocation *lastLocation = (self.lastRecordedLocation != nil) ? self.lastRecordedLocation : oldLocation;
CLLocation *bestLocation = nil;
CGFloat bestAccuracy = kRequiredHorizontalAccuracy;
for (CLLocation *location in self.locationHistory) {
if ([NSDate timeIntervalSinceReferenceDate] - [location.timestamp timeIntervalSinceReferenceDate] <= kValidLocationHistoryDeltaInterval) {
if (location.horizontalAccuracy < bestAccuracy && location != lastLocation) {
bestAccuracy = location.horizontalAccuracy;
bestLocation = location;
}
}
}
if (bestLocation == nil) bestLocation = newLocation;
CLLocationDistance distance = [bestLocation distanceFromLocation:lastLocation];
if (canUpdateDistance) self.totalDistance += distance;
self.lastRecordedLocation = bestLocation;
}
}
}
Non dimenticare di accettarlo come risposta per non farlo apparire come una domanda aperta. Bel esempio, a proposito. –
Ciao Daniel, sto recuperando lo stesso problema da più di una settimana e ancora non ho trovato alcuna soluzione. Quindi, puoi aiutarmi? –
@Daniel .... Aiutatemi, per favore. –