Quantcast
Channel: Delphi Forum - Delphi Programming Kings of Code - Delphi Programming
Viewing all articles
Browse latest Browse all 173

Calculates the distance between two points on Earth

$
0
0
//With the code below you can calculate the distance between two points on the map

Const
          pi      = 3.14159265358979323846;
          half_pi = 1.57079632679489661923;
          rad     = 3.14159265358979323846/180;{conversion factor for degrees into radians}
   
Function atan2(y,x: extended): extended;
    begin 
      if x = 0.0 then
        begin
          if y = 0.0 then
            (* Error! Give error message and stop program *)
          else if y > 0.0 then
            atan2 := half_pi
          else
            atan2 := -half_pi
        end
      else
        begin
          if x > 0.0 then
              atan2 := arctan(y/x)
          else if x < 0.0 then
            begin
              if y >= 0.0 then
                atan2 := arctan(y/x)+pi
              else
                atan2 := arctan(y/x)-pi
            end; 
        end;
    end; {atan2}


//For Teste

Var
   radius,lon1,lon2,lat1,lat2,dlon,dlat,a,distance :extended;

Begin
   radius:=6378.137; {Earth equatorial radius in Km; as used in most GPS} 

   {Input here coordinates of the two points:}

   lon1:=-99.133333; {The Zocalo square, Mexico City}
   lat1:=19.432778;
   
   lon2:=-99.1862;   {The National Museum of Anthropology, Mexico City}
   lat2:=19.4260;

 {The Haversine formula}

   dlon:= (lon2-lon1)*rad;
   dlat:= (lat2-lat1)*rad;
   a:= sqr(sin(dlat/2)) + cos(lat1*rad)*cos(lat2*rad)*sqr(sin(dlon/2));
   distance:= radius*(2*atan2(sqrt(a),sqrt(1-a)));
  ShowMessage('Distance: '+ FloatToStr( round(distance*1000)) +'metros');
   End.
 


 

Viewing all articles
Browse latest Browse all 173

Trending Articles