Delphi 根据经纬度计算地球上两点之间的距离

 

EARTH_RADIUS = 6378137.0;    //单位M

function getRad(d: Double): Double;
begin
  Result := d * PI / 180.0;
end;

function GetFlatternDistance(lat1, lng1, lat2, lng2: Double): Double;
var
  f, g, l: Double;
  sg, sl, sf: Double;
  s, c, w, r, d, h1, h2: Double;
  a, fl: Double;
begin
  f := getRad((lat1 + lat2)/2);
  g := getRad((lat1 - lat2)/2);
  l := getRad((lng1 - lng2)/2);
          
  sg := sin(g);
  sl := sin(l);
  sf := sin(f);

  a := EARTH_RADIUS;
  fl := 1/298.257;
          
  sg := sg*sg;
  sl := sl*sl;
  sf := sf*sf;

  s := sg*(1-sl) + (1-sf)*sl;
  c := (1-sg)*(1-sl) + sf*sl;

  w := ArcTan(sqrt(s/c));
  r := sqrt(s*c)/w;
  d := 2 * w * a;
  h1 := (3*r -1)/2/c;
  h2 := (3*r +1)/2/s;

  Result := d*(1 + fl*(h1*sf*(1-sg) - h2*(1-sf)*sg));
end;

你可能感兴趣的:(Delphi 根据经纬度计算地球上两点之间的距离)