Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Tom Coomer
1,331 PointsHow to calculate distance between two longitude and latitude points.
How do I calculate the distance between two longitude and latitude points in Swift.
I have the users current location and need to work out the distance between another location.
Thanks
3 Answers

James Barnett
39,199 PointsThe easiest way to do this is to use the Haversine Formula
Here's some code to get you started
+ (CGFloat)directMetersFromCoordinate:(CLLocationCoordinate2D)from toCoordinate:(CLLocationCoordinate2D)to {
static const double DEG_TO_RAD = 0.017453292519943295769236907684886;
static const double EARTH_RADIUS_IN_METERS = 6372797.560856;
double latitudeArc = (from.latitude - to.latitude) * DEG_TO_RAD;
double longitudeArc = (from.longitude - to.longitude) * DEG_TO_RAD;
double latitudeH = sin(latitudeArc * 0.5);
latitudeH *= latitudeH;
double lontitudeH = sin(longitudeArc * 0.5);
lontitudeH *= lontitudeH;
double tmp = cos(from.latitude*DEG_TO_RAD) * cos(to.latitude*DEG_TO_RAD);
return EARTH_RADIUS_IN_METERS * 2.0 * asin(sqrt(latitudeH + tmp*lontitudeH));
}
from: http://www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe#Objective_C

notf0und
11,940 PointsThis website details lots of different methods in JavaScript, but it's the equations you'll want: http://www.movable-type.co.uk/scripts/latlong.html
Good luck! Hope this helps.

Andres Oliva
7,810 PointsAndres Oliva
7,810 PointsI'm just commenting because I want to know how too :P