[ad_1]
Aim
I’m at the moment attempting to get the information to plot an elevation chart for a HKWorkout. I would like the X-axis to symbolize distance and the Y-axis to symbolize elevation. This kind of graph is pretty frequent and acts as a kind of cross part of a exercise route.
It is a tough mockup of the kind of chart I’m hoping to plot.
Progress
I’ve efficiently queried the WorkoutRoute for a given HKWorkout and I’m able to get the entire recorded CLLocations within the route.
func getWorkoutRoute(for exercise: HKWorkout) async -> [CLLocation]? {
let byWorkout = HKQuery.predicateForObjects(from: exercise)
let samples = attempt? await withCheckedThrowingContinuation { (continuation: CheckedContinuation<[HKSample], Error>) in
retailer.execute(HKAnchoredObjectQuery(kind: HKSeriesType.workoutRoute(), predicate: byWorkout, anchor: nil, restrict: HKObjectQueryNoLimit, resultsHandler: { (question, samples, deletedObjects, anchor, error) in
if let hasError = error {
continuation.resume(throwing: hasError); return
}
guard let samples = samples else { return }
continuation.resume(returning: samples)
}))
}
guard let route = (samples as? [HKWorkoutRoute])?.first else { return nil }
let areas = attempt? await withCheckedThrowingContinuation { (continuation: CheckedContinuation<[CLLocation], Error>) in
var allLocations = [CLLocation]() // constructed up over time as and when HK tells us
retailer.execute(HKWorkoutRouteQuery(route: route) { (question, locationsOrNil, carried out, errorOrNil) in
// This block could also be referred to as a number of instances.
if let error = errorOrNil {
continuation.resume(throwing: error); return
}
guard let areas = locationsOrNil else {
fatalError("Invalid State: This will solely fail if there was an error.")
}
allLocations += areas
if carried out {
continuation.resume(returning: allLocations)
}
})
}
return areas
}
Drawback
Inside every CLLocation I’m able to entry the elevation (so that is the y-axis sorted) however I’m struggling to learn how to get the gap traveled for the given location.
CLLocation does has a technique to get the gap between two areas however that is calculated because the crow flies
so is not going to give correct values. The areas additionally embrace a timestamp.
The HKWorkout objects ought to embrace all the information I have to acutely get the gap I am simply undecided how.
Different well being apps similar to HealthFit are in a position to present get the information I’m searching for so I do know it should be potential.
Can anybody level me in the proper route?
[ad_2]