Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@garmstro
Created October 7, 2015 18:02
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save garmstro/29ef8de111ba69453ac6 to your computer and use it in GitHub Desktop.
Save garmstro/29ef8de111ba69453ac6 to your computer and use it in GitHub Desktop.
/**
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
}
@ajaysinghthakur
Copy link

ajaysinghthakur commented Feb 7, 2018

value of radiusCorrection in line elevation = radiansToDegrees(elevAngle - distance / radiusCorrection)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment