Created
February 12, 2019 03:30
-
-
Save atetlaw/021e059712f3229dfa5cce82def81c31 to your computer and use it in GitHub Desktop.
Trying to extend CoreLocation types to provide `Measurement` values. Of course all of these types just `typealias` `Double`, so they're all extending `Double`!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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