HealthKit running distance does not add up correctly?

∥☆過路亽.° 提交于 2021-02-11 14:21:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!