Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Trying to extend CoreLocation types to provide `Measurement` values. Of course all of these types just `typealias` `Double`, so they're all extending `Double`!
extension CLLocationDegrees {
public var degrees: Measurement<UnitAngle> {
return Measurement(value: self, unit: .degrees)
}
public var radians: Measurement<UnitAngle> {
return degrees.converted(to: .radians)
}
public var degreesMinutes: (Measurement<UnitAngle>, Measurement<UnitAngle>) {
let deg = abs(self)
let d = floor(deg)
let m = (deg - d) * 60
return (Measurement(value: d, unit: .degrees), Measurement(value: m, unit: .arcMinutes))
}
public var degreesMinutesSeconds: (Measurement<UnitAngle>, Measurement<UnitAngle>, Measurement<UnitAngle>) {
let deg = abs(self)
let d = floor(deg)
let ms = (deg - d) * 60
let m = floor(ms)
let s = (ms - m) * 60
return (Measurement(value: d, unit: .degrees), Measurement(value: m, unit: .arcMinutes), Measurement(value: s, unit: .arcSeconds))
}
}
extension CLLocationSpeed {
public var metersPerSecond: Measurement<UnitSpeed> {
return Measurement(value: self, unit: .metersPerSecond)
}
}
extension CLLocationDistance {
public var meters: Measurement<UnitLength> {
return Measurement(value: self, unit: .meters)
}
}
extension CLLocationDirection {
public var bearing: Measurement<UnitAngle> {
return Measurement(value: self, unit: .degrees)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.