Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ChrisLowe-Takor/d7fa7e75d86b974a3157 to your computer and use it in GitHub Desktop.
Save ChrisLowe-Takor/d7fa7e75d86b974a3157 to your computer and use it in GitHub Desktop.
extension Double {
var degreesToRadians: CGFloat {
return CGFloat(self) * CGFloat(M_PI) / 180.0
}
}
extension CLLocationCoordinate2D {
func distanceFromCoordinateInMeters(coordinate: CLLocationCoordinate2D) -> Double {
// Haversine forumla. https://en.wikipedia.org/wiki/Haversine_formula
let earthRadius: Double = 6371.01
let nDLat = (coordinate.latitude - self.latitude).degreesToRadians
let nDLon = (coordinate.longitude - self.longitude).degreesToRadians
let fromLat = self.latitude.degreesToRadians
let toLat = coordinate.latitude.degreesToRadians
let nA = pow ( sin(nDLat/2), 2 ) + cos(fromLat) * cos(toLat) * pow ( sin(nDLon/2), 2 )
let nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA ))
let nD = earthRadius * Double(nC)
return nD * 1000.0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment