Created
October 7, 2015 18:02
-
-
Save garmstro/29ef8de111ba69453ac6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
Calculate initial compass bearing between two locations | |
- parameter fromLocation: Source Location | |
- parameter toLocation: Destination Location | |
- returns: bearing (CLLocationDirection) | |
*/ | |
private func bearingFromLocation(fromLocation: CLLocation, toLocation: CLLocation) -> CLLocationDirection { | |
var bearing: CLLocationDirection | |
let fromLat = degreesToRadians(fromLocation.coordinate.latitude) | |
let fromLon = degreesToRadians(fromLocation.coordinate.longitude) | |
let toLat = degreesToRadians(toLocation.coordinate.latitude) | |
let toLon = degreesToRadians(toLocation.coordinate.longitude) | |
let y = sin(toLon - fromLon) * cos(toLat) | |
let x = cos(fromLat) * sin(toLat) - sin(fromLat) * cos(toLat) * cos(toLon - fromLon) | |
bearing = radiansToDegrees( atan2(y, x) ) as CLLocationDirection | |
bearing = (bearing + 360.0) % 360.0 | |
return bearing | |
} | |
/** | |
Calculate final bearing between two locations | |
- parameter fromLocation: Source Location | |
- parameter toLocation: Destination Location | |
- returns: bearing (CLLocationDirection) | |
*/ | |
private func finalBearingFromLocation(fromLocation: CLLocation, toLocation: CLLocation) -> CLLocationDirection { | |
var bearing: CLLocationDirection | |
let fromLat = degreesToRadians(fromLocation.coordinate.latitude) | |
let fromLon = degreesToRadians(fromLocation.coordinate.longitude) | |
let toLat = degreesToRadians(toLocation.coordinate.latitude) | |
let toLon = degreesToRadians(toLocation.coordinate.longitude) | |
let y = sin(fromLon - toLon) * cos(fromLat) | |
let x = cos(toLat) * sin(fromLat) - sin(toLat) * cos(fromLat) * cos(fromLon - toLon) | |
bearing = radiansToDegrees( atan2(y, x) ) as CLLocationDirection | |
bearing = (bearing + 180.0) % 360.0 | |
return bearing | |
} | |
/** | |
Calculate angle of elevation between two locations | |
- parameter fromLocation: Source Location | |
- parameter distance: Destination Distance | |
- parameter altitude: Destination Altitude | |
- returns: elevation (Double) | |
*/ | |
private func elevationFromLocation(fromLocation: CLLocation, toLocation: CLLocation) -> Double { | |
var elevation: Double | |
let elevDiff = toLocation.altitude - fromLocation.altitude | |
let distance = toLocation.distanceFromLocation(fromLocation) | |
let elevAngle = atan2(elevDiff, distance) | |
elevation = radiansToDegrees(elevAngle - distance / radiusCorrection) | |
return elevation | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
value of radiusCorrection in line elevation = radiansToDegrees(elevAngle - distance / radiusCorrection)