Created
January 12, 2016 09:36
-
-
Save ChrisLowe-Takor/d7fa7e75d86b974a3157 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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