3

I have an HKLiveWorkoutBuilder to track an outdoor run. I also have a LocationManager to add locations to the workout with an HKWorkoutRouteBuilder.

I get HKQuantityTypeIdentifier.distanceWalkingRunning by the workoutBuilder(_ workoutBuilder: HKLiveWorkoutBuilder, didCollectDataOf collectedTypes: Set<HKSampleType>) delegate method. Can anyone tell me, if these values are only generated by CoreMotion or does Apple also take the CLLocations into account that I added? Since you get this value also without having GPS, I'm not really sure if Apple is taking GPS into acount for calculating the distance.

If not, how can I add the distance calculated by my LocationManager to my workout?

iVentis
  • 993
  • 6
  • 19

1 Answers1

0

distanceWalkingRunning is a quantity type that is collected (or not) by the HKLiveWorkoutBuilder.dataSource.

When you create your HKLiveWorkoutBuilder, you set the datasource property to an HKLiveWorkoutDataSource instance that you also create using

init(healthStore: HKHealthStore, workoutConfiguration: HKWorkoutConfiguration?)

That dataSource has a property, typesToCollect, that will tell you which samples are being automatically collected and sent to your workoutBuilder.

This changes depending on the configuration that you initialize the datasource with (ie outdoor run, outdoor walk, etc...)

If you are happy with those, then you can leave them and make sure you have read access to those values so that they are associated with your workout. If you aren't, you can disable some or all of them, and enable others, using the

func enableCollection(for: HKQuantityType, predicate: NSPredicate?)

and

func disableCollection(for: HKQuantityType)

functions of the dataSource.

In answer to your question about how the running distance is calculated, we can assume that they use GPS in combination with motion, but how they do it is opaque. I am positive they are using GPS, but I how they calculate the values exactly is opaque. If you are creating a running app, I would rely on their values and use the GPS locations to construct the route. You will need to smooth that GPS data as well.

Daniel K
  • 1,119
  • 10
  • 20