I\'ve looked around and still couldn\'t find anything to help me really! I\'ve written a program to calculate the distance between 2 cities using their Latitudes and Longitu
for people who need in swift:
// Haversine formula:
func deg2rad(_ deg: Double) ->Double {
return deg * Double.pi / 180.0
}
func distanceEarth(lat1d: Double, lon1d: Double, lat2d: Double, lon2d: Double) ->Double {
let earthRadiusKm = 6371.0
let lat1r = deg2rad(lat1d);
let lon1r = deg2rad(lon1d);
let lat2r = deg2rad(lat2d);
let lon2r = deg2rad(lon2d);
let u = sin((lat2r - lat1r)/2);
let v = sin((lon2r - lon1r)/2);
return 2.0 * earthRadiusKm * asin(sqrt(u * u + cos(lat1r) * cos(lat2r) * v * v));
}
//test here.... https://andrew.hedges.name/experiments/haversine/
func doTestHaversine(){
let km = distanceEarth(lat1d: 38.898556, lon1d: -77.037852, lat2d: 38.897147, lon2d: -77.043934)
print(km) // should show : 0.549 or similar..
}