1
$\begingroup$

Question is also related to programming, but maybe I can solve it here. I am using distance calculation using Haversine method(probably it's not important, but I will post the function):

double GeoDistance(GeoCoordinate* p1, GeoCoordinate* p2)
{
  const float two=2.0;

  double lat1 = p1->lat * factor;
  double lon1 = p1->lon * factor;
  double lat2 = p2->lat * factor;
  double lon2 = p2->lon * factor;

  return RADIUS_METER * two * asin(sqrt(square(sin((lat1-lat2)/two)) + cos(lat1)*cos(lat2)*square(sin((lon2-lon1)/two))));
}

This gives me suitable precision. Since I am using this code in tiny microcontroller for number of geo points, I need to optimize this function with some other, which will work faster but precision can suffer. To give an idea on how much precision can suffer I will tell that the approximate distance I am calculating is about 200-400m and it can easily be +- 100m.

  • 0
    "The approximate distance I am calculating is about 200-400m and it can easily be +- 100m." Eureka! Always return 300. :)2012-11-19
  • 0
    hehe :) Can't do that "always", need some simple formula to tell me when exactly to return 300 :) Hope the one mentioned by dmuir is good enough.2012-11-19

1 Answers 1

1

For distances less than a kilometre the C code below gives the same answer as the haversine formula to within 4.5 cm ( and 4.5m for distances up to 10km) (lats and longs in radians; R is the radius of the earth)

double dlat = lat2-lat1;
double dlon = remainder( lon2-lon1, 2.0*PI);
    return R*hypot( dlat, dlon*cos(lat1));

(Note that the complex calculation of difference of longitude is required in case the points lie on opposite sides of 180 degrees east).