Skip to content

Instantly share code, notes, and snippets.

@Jamonek
Last active May 12, 2019
Embed
What would you like to do?
// 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