Einzelnen Beitrag anzeigen

Benutzerbild von GPRSNerd
GPRSNerd

Registriert seit: 30. Dez 2004
Ort: Ruhrpott
239 Beiträge
 
Delphi 10.4 Sydney
 
#3

AW: GMLib (Google Map), wer kennt sich aus?

  Alt 8. Nov 2014, 18:19
Folgenden Code benutze ich zur Distanzberechnung mittels Haversine-Formel (http://en.wikipedia.org/wiki/Haversine_formula) auf der Erdkugel zwischen zwei GPS-Koordinaten, der ist recht genau:

Delphi-Quellcode:
const
  EarthRadius = 6371.0; //Mean Earth radius in Km
  pi = System.Pi;
  half_pi = pi/2;
  rad = pi/180; //conversion factor for degrees into radians

Function atan2(const y, x: Extended): Extended;
begin
  Result := 0.0; //temp.

  if x = 0.0 then
  begin
    if y = 0.0 then
      raise EMathError.Create('Math Error in atan2: undefined for y=x=0')
    else if y > 0.0 then
      Result := + half_pi
    else
      Result := - half_pi;
  end
  else
  begin
    if x > 0.0 then
        Result := arctan(y/x)
    else if x < 0.0 then
    begin
      if y >= 0.0 then
        Result := arctan(y/x)+pi
      else
        Result := arctan(y/x)-pi;
    end;
  end;
end;

Function CalcDistance(const lonFrom, lonTo, latFrom, latTo: Extended): Extended;
Var
   dlon, dlat, a, c: Extended;
begin
  if ((latFrom = latTo) and (lonFrom = lonTo)) then //Trivialfall, identische Punkte
    Result := 0.0
  else
  begin
    dlon := (lonTo-lonFrom)*rad;
    dlat := (latTo-latFrom)*rad;

    //The Haversine formula
    a:= sqr(sin(dlat/2)) + cos(latFrom*rad) * cos(latTo*rad) * sqr(sin(dlon/2));
    try
      c := 2 * atan2(sqrt(a), sqrt(1-a));
    except
      c := 0;
    end;

    //Distanz/Großkreis auf dem Erdradius
    Result := EarthRadius * c;
  end;
end;
Stefan
  Mit Zitat antworten Zitat