本文介绍了快速获取每公里分钟数,从每米秒数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 CLLocations 数组,每个数组具有每米秒数的元素。我想从该数组计算运行拆分(每公里分钟)。如何从 CLLocations 数组中获取每公里的分钟数?

I have an array of CLLocations each with the seconds per meter element. I want to calculate the run splits from that array (Minutes per kilometer). How can I fetch the minutes per kilometer for each kilometer from the CLLocations array?

这就是我在下面获取位置的方法。

This is how I am getting the locations below.

let query = HKWorkoutRouteQuery(route: route) { (_, locations, _, error) in
        if let error = error {
            print("There was an error fetching route data: ", error)
            return
        }

        guard let locations = locations else { return }
    }
    healthStore.execute(query)


推荐答案

CLLocationSpeed 定义为以米/秒为单位的速度,请参见

CLLocationSpeed is defined as speed in meters per second, see Apple Docs

它是 Double 的别名,因此您可以将其翻译为:

It is an alias for Double, so you can just translate it with:

let speedMS = location.speed
let speedKMM = speedMS * 3 / 50

您可以使用扩展名以提高代码的可读性:

You can use an extension for better code readability:

extension CLLocationSpeed {
    func toKmM() -> Double {
        return self * 3 / 50
    }
}

当您要获得km / m,只需使用 CLLocation.speed.toKmM()

And when you want to get km/m, you just use CLLocation.speed.toKmM()

编辑:

甚至@Leo Dabus提出的更简单的解决方案,都可以扩展CLLocation:


And even simpler solution by @Leo Dabus, extend CLLocation:

extension CLLocation { 
    var kilometersPerMinute: Double { 
        speed * 0.06 
    } 
}

这篇关于快速获取每公里分钟数,从每米秒数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 12:45