Skip to content

Instantly share code, notes, and snippets.

@Jamonek
Last active May 12, 2019 19:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jamonek/16ecda78cebcd0da5862 to your computer and use it in GitHub Desktop.
Save Jamonek/16ecda78cebcd0da5862 to your computer and use it in GitHub Desktop.
// The fixed formula
struct Global {
static var schoolID: Int? // Work around for passing the selected location ID
static var userCoord: CLLocationCoordinate2D?
func computeDistance() -> Double {
// User coordinate
let uLat: Double = 35.6603676639829
let uLng: Double = -80.4443005566303
// School coordinate
let sLat: Double = 35.523075
let sLon: Double = -80.54142
let radius: Double = 3959.0 // Miles
let deltaP = (sLat.degreesToRadians - uLat.degreesToRadians)
let deltaL = (sLon.degreesToRadians - uLng.degreesToRadians)
let a = sin(deltaP/2) * sin(deltaP/2) + cos(uLat.degreesToRadians) * cos(sLat.degreesToRadians) * sin(deltaL/2) * sin(deltaL/2)
let c = 2 * atan2(sqrt(a), sqrt(1-a))
let d = radius * c
return d.roundToPlaces(2) // distance in miles rounded to 2 decimal places
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment