问题
If I use Apple Health Kit to fetch the route data for a given running workout, and add up distance between each sample, it does not match up with the Health App total distance. I don't understand why. Please check the blow simplified test code.
Only thing I can think of, is that I don't take pauses and GPS dropouts into account in the code below - but this should make the computed distance longer, I think. I have workouts where the computed distance is 50-100 meters short (10km workout, no pauses), and examples where the computed distance is longer that what the Health app says.
double test = 0;
CLLocation* lastLocation;
- (void)readLocationPointsFromHealthStoreForWorkoutRoute:(HKWorkoutRoute*)route withActivityId:(NSString*)activityId
{
NSMutableDictionary* locations = [[NSMutableDictionary alloc] init]; // arrays of locations stored in the health store, key is the activity ID
NSMutableArray<NSDictionary*>* coordinates = [[NSMutableArray alloc] init];
test = 0;
HKWorkoutRouteQuery* query = [[HKWorkoutRouteQuery alloc] initWithRoute:route dataHandler:^(HKWorkoutRouteQuery* query, NSArray<CLLocation*>* routeData, BOOL done, NSError* error)
{
for(int i = 1; i < routeData.count; i++){
if(lastLocation){
CLLocationDistance distance = [routeData[i] distanceFromLocation:lastLocation];
test += distance;
}
lastLocation = routeData[i];
}
}];
[[HealthKit sharedHealthStore] executeQuery:query];
}
来源:https://stackoverflow.com/questions/65296880/healthkit-running-distance-does-not-add-up-correctly