Calculating distance in Objective-C

Returns distance in meters between to coordinates.

  1.  
  1. float calcDistance(float lat1, float lon1, float lat2,float lon2)
  2. {
  3.         float R = 6371; // km
  4.         float dLat = deg2rad((lat2-lat1));
  5.         float dLon = deg2rad((lon2-lon1));
  6.         float a = sin(dLat/2.0) *
  7. sin(dLat/2.0)+cos(deg2rad(lat1))*
  8. cos(deg2rad(lat2))*sin(dLon/2.0)*sin(dLon/2.0);
  9.         float c = 2.0 * atan2(sqrt(a), sqrt(1-a));
  10.         float d = R * c;
  11.  
  12.         return d*1000;
  13. }