Calculate distance between two points by latitude and longitude

11

I need to calculate the distance in kilometers (km) between two points through their latitude and longitude. I have not found an effective way to do it.

The latitudes and longitudes that I have are formatted as follows:

  

lat: -29.6518875

     

long: -51.1716005

I found ways to calculate them when they are in degrees, but nothing when they are in this format.

    
asked by anonymous 19.10.2016 / 21:17

2 answers

8

Try this code with me:

function sgn(a: real): real;
begin
  if a < 0 then sgn := -1 else sgn := 1;
end;

function atan2(y, x: real): real;
begin
  if x > 0  then atan2 := arctan(y/x)
  else if x < 0  then atan2 := arctan(y/x) + pi
  else atan2 := pi/2 * sgn(y);
end;

procedure TForm1.Button2Click(Sender: TObject);
var Long1, Lat1, Long2, Lat2, Val1, Val2, Long, Lat: Double;
begin
  Lat1 := 41.500605;
  Long1 := -7.731313;
  Lat2 := 37.186851;
  Long2 := -8.742056;

  Lat1 := Lat1 * pi / 180.0;
  Long1 := Long1 * pi / 180.0;
  Lat2 := Lat2 * pi / 180.0;
  Long2 := Long2 * pi / 180.0;

  Lat := Lat2 - Lat1;
  Long := Long2 - Long1;

  Val1 := 6371.0;
  Val2 := sin(Lat / 2) * sin(Lat / 2) + cos(Lat1) * cos(Lat2) * sin(Long / 2) * sin(Long / 2);
  Val2 := 2 * ATan2(sqrt(Val2), sqrt(1 - Val2));

  Memo1.Lines.Add(FloatToStr(Val1 * Val2) + ' KM');
end;

Any questions or doubts, please refer to Great-circle distance .

    
20.10.2016 / 16:25
1

Follow the complete code based on the contribution of the Tmc colleague, ready to test

On a blank form:

Copy the code below and press Ctrl + V (Paste)

object Edit1: TEdit
  Touch.InteractiveGestures = [LongTap, DoubleTap]
  TabOrder = 0
  KeyboardType = NumberPad
  Text = '41.500605'
  Position.X = 56.0
  Position.Y = 56.0
  TextPrompt = 'Latitude1'
end
object Edit2: TEdit
  Touch.InteractiveGestures = [LongTap, DoubleTap]
  TabOrder = 1
  KeyboardType = NumberPad
  Text = '-7,731313'
  Position.X = 56.0
  Position.Y = 80.0
  TextPrompt = 'Longitude1'
end
object Edit3: TEdit
  Touch.InteractiveGestures = [LongTap, DoubleTap]
  TabOrder = 2
  KeyboardType = NumberPad
  Text = '37,186851'
  Position.X = 56.0
  Position.Y = 128.0
  TextPrompt = 'Latitude2'
end
object Edit4: TEdit
  Touch.InteractiveGestures = [LongTap, DoubleTap]
  TabOrder = 3
  KeyboardType = NumberPad
  Text = '-8,742056'
  Position.X = 56.0
  Position.Y = 152.0
  TextPrompt = 'Longitude2'
end
object Button1: TButton
  Position.X = 56.0
  Position.Y = 184.0
  TabOrder = 4
  Text = 'Distancia'
  OnClick = Button1Click
end
object Label1: TLabel
  Position.X = 56.0
  Position.Y = 40.0
  Text = 'Coordenada A'
end
object Label2: TLabel
  Position.X = 56.0
  Position.Y = 112.0
  Text = 'Coordenada B'
end
object Label3: TLabel
  Position.X = 56.0
  Position.Y = 216.0
end

Place the following code below implementation , then press Shift + Ctrl + C to create the signatures

  function CalcDistanciaCoord(Lat1,Lng1,Lat2,Lng2:Double):Double;//Retorna distancia em Km
    const
           r:Double = 6371.0;//Raio da terra
    var
           Val, Lng, Lat: Double;
           piLat1,piLng1,piLat2,piLng2:Double;

    function sgn(a: real): real;
    begin
    //if a < 0 then sgn := -1 else sgn := 1;
        result := a/abs(a)
    end;

    function atan2(y, x: real): real;
    begin
      if x > 0  then atan2 := arctan(y/x)
      else if x < 0  then atan2 := arctan(y/x) + pi
          else atan2 := pi/2 * sgn(y);
    end;

begin
  piLat1 := Lat1 * pi / 180.0;
  piLng1 := Lng1 * pi / 180.0;
  piLat2 := Lat2 * pi / 180.0;
  piLng2 := Lng2 * pi / 180.0;

  Lat := Lat2 - Lat1;
  Lng := Lng2 - Lng1;

  Val := sin(Lat / 2) * sin(Lat / 2) + cos(Lat1) * cos(Lat2) * sin(Lng / 2) * sin(Lng / 2);
  Val := 2 * ATan2(sqrt(Val), sqrt(1 - Val));

  Result:= r * Val;
end;

procedure TForm1.Button1Click(Sender: TObject);
var d,La1,Lo1,La2,Lo2:Double;
    begin
    La1 := StrToFloat(Edit1.text);
    Lo1 := StrToFloat(Edit2.text);
    La2 := StrToFloat(Edit3.text);
    Lo2 := StrToFloat(Edit4.text);
    d:=CalcDistanciaCoord(La1,Lo1,La2,Lo2);
    label3.text:=formatfloat('#0.000000 Km',d);
    //label1.caption:=formatfloat('#0.000000 Km",d);//VCL
    end;
    
27.01.2017 / 20:53