{'------------------------------------------
Private Const PI = 3.1415926 'Pi
Private m_R As Double
Public Enum Distance_Mode '
Miles = 0
Feet = 1
End Enum
Private Type PointType
lat As Double
lon As Double
End Type
Private m_PointA As PointType
Private m_PointB As PointType
Private m_DistMode As Distance_Mode
'to get the parameters of DistanceMode
Public Property Get Dist_Mode() As Distance_Mode
Dist_Mode = m_DistMode
End Property
Public Property Let Dist_Mode(ByVal vNewValue As Distance_Mode)
m_DistMode = vNewValue
End Property
Private Sub Class_Initialize()
m_R = (R_A + R_B) / 2
End Sub
'get the first point
Public Function SetPointA(lat As Double, lon As Double) As Boolean
If (lat <= 90 And lat >= -90) And (lon >= -180 And lon <= 180) Then
m_PointA.lat = lat
m_PointA.lon = lon
SetPointA = True
Else
SetPointA = False
End If
End Function
'get the second point
Public Function SetPointB(lat As Double, lon As Double) As Boolean
If (lat <= 90 And lat >= -90) And (lon >= -180 And lon <= 180) Then
m_PointB.lat = lat
m_PointB.lon = lon
SetPointB = True
Else
SetPointB = False
End If
End Function }
//calculate the distance
function GetDistance(lan1,lon1, lan2, lon2: Double) : Double;
var
angAB: Double;
tempV: Double;
tempD: Double ;
tempV := Cos(angLatA) * Cos(angLatB) * Cos(angLonB - angLonA) + Sin(angLatA) * Sin(angLatB);
if (tempV <> 1) and (tempV <> -1) then
begin
angAB := ArcTan(-tempV /Sqr(-tempV * tempV + 1)) + 2 * ArcTan(1) ;
tempD := m_R * angAB;
end;
//angAB := 0
if tempV = 1 then tempD := 0;
//angAB := 180
if tempV = -1 then tempD := m_R * PI ;
{if m_DistMode = Miles then
tempD := tempD / 1609
else
tempD := tempD / 1609 * 5280; }
Result := tempD;
end;
function EarthDis(lan1, lon1, lan2, lon2: Double): Double;
var
D: double ;
tmpVal: double ;
fi1,fi2: double ;
drda: double;
tmp1,tmp2: double ;
begin
drda := lon2 - lon1;
//drda := drda/60.0;// in degrees
drda := drda*PI/180.0;// in radians
fi1 := lan1;
fi2 := lan2;
fi1 := fi1 * PI / 180.0;//in in radians
fi2 := fi2 * PI / 180.0;//in in radians
tmpVal:=sin(fi1)*sin(fi2)+cos(fi1)*cos(fi2)*cos(drda);
{if(fabs(tmpVal)>1.0) then
begin
AfxMessageBox("Invalidate input valve of arccos!",MB_ICONSTOP);
return;
end; }
D:=arccos(tmpVal);// in radians
//dis:=(dis*180.0/PI)*60.0; // in minutes