Skip to content

Instantly share code, notes, and snippets.

@elijahzarlin
Last active September 13, 2018 18:21
Show Gist options
  • Select an option

  • Save elijahzarlin/319a63a0bae31545274c99624b451716 to your computer and use it in GitHub Desktop.

Select an option

Save elijahzarlin/319a63a0bae31545274c99624b451716 to your computer and use it in GitHub Desktop.
world scale ar post
func azimuthBetween(origin: CLLocation, destination: CLLocation) -> Float {
var azimuth: Float = 0
let originLatitude = GLKMathDegreesToRadians(Float(origin.coordinate.latitude))
let originLongitude = GLKMathDegreesToRadians(Float(origin.coordinate.longitude))
let destinationLatitude = GLKMathDegreesToRadians(Float(destination.coordinate.latitude))
let destinationLongitude = GLKMathDegreesToRadians(Float(destination.coordinate.longitude))
let dLon = destinationLongitude - originLongitude
let y = sin(dLon) * cos(destinationLatitude)
let x = cos(originLatitude) * sin(destinationLatitude) - sin(originLatitude) * cos(destinationLatitude) * cos(dLon)
azimuth = atan2(y, x)
if(azimuth < 0) { azimuth += 2 * .pi } // We only use positive values for consistency
return azimuth
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment