The simFlight Network Forums

# ETA programming struggle

## Recommended Posts

Hey Team,

I have been racking my brain for the past 2 days trying to deep dive into programming an accurate and effective ETA system.

Using VB.net and the harvestine theory formula to calculate the remaining time until destination. I am pulling the user's current Position (latitude and longitude) via a timer then inputting that into the following:

```Public Sub CalculateAndDisplayETA()

Dim xmlFile As String = My.Settings.ApplicationDirectory + "\Routes\" + IFETabletLrg.DepartureLbl.Text + IFETabletLrg.ArrivalLbl.Text + ".xml"

' Load the XML file that contains route information
Dim xmlDoc As New XmlDocument()

' Find the <destination> element
Dim destinationNode As XmlNode = xmlDoc.SelectSingleNode("/OFP/destination")

' Check if the <destination> elements exist
If destinationNode IsNot Nothing Then
' Extract the values from the <destination> element
Dim destinationAirportLat As Double = Double.Parse(destinationNode.SelectSingleNode("pos_lat").InnerText)
Dim destinationAirportLon As Double = Double.Parse(destinationNode.SelectSingleNode("pos_long").InnerText)

Dim startLat As Double = IFETabletLrg.UserPosLat  ' Formula Start latitude in degrees (Current User's A/C Lat)
Dim startLon As Double = IFETabletLrg.userPosLng    ' Formula Start longitude in degrees (Current User's A/C Long)

Dim endLat As Double = destinationAirportLat   ' Destination latitude in degrees
Dim endLon As Double = destinationAirportLon   ' Destination longitude in degrees
Dim speed As Double = ((gspeed.Value / 33714.6311111111) * 1.852)  ' User's Groundspeed

' Calculate the distance using the Haversine formula
Dim distanceNM As Double = HaversineDistance(startLat, startLon, endLat, endLon)

' Convert distance to nautical miles to kilometers
Dim distanceKm As Double = distanceNM * 1.852

' Calculate the ETA in hours
Dim timeHours As Double = distanceKm / speed

' Calculate hours and minutes from the total hours (timeHours)
Dim hours As Integer = CInt(Math.Floor(timeHours))
Dim minutes As Integer = CInt(Math.Round((timeHours - hours) * 60))

' Format the ETA as "hh'h mm'm"
Dim formattedETA As String = \$"{hours:00}h {minutes:00}m"

' Display the formatted ETA in the label
IFETabletLrg.ETALbl.Text = formattedETA
End If
End Sub

Private Function HaversineDistance(startLat As Double, startLon As Double, endLat As Double, endLon As Double) As Double
Dim startLatRad As Double = startLat * Math.PI / 180.0
Dim startLonRad As Double = startLon * Math.PI / 180.0
Dim endLatRad As Double = endLat * Math.PI / 180.0
Dim endLonRad As Double = endLon * Math.PI / 180.0

' Earth's radius in nautical miles
Const EarthRadiusNM As Double = 3443.8445

' Haversine formula
Dim a As Double = Math.Sin(dLat / 2.0) * Math.Sin(dLat / 2.0) + Math.Cos(startLatRad) * Math.Cos(endLatRad) * Math.Sin(dLon / 2.0) * Math.Sin(dLon / 2.0)
Dim c As Double = 2.0 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a))
Dim distanceNM As Double = EarthRadiusNM * c

Return distanceNM
End Function```

This is returning a result however it seems to be inaccurate when compared to other software such as SmartCars. It is slower by a range of 30 minutes to 90 minutes.

Any ideas?

Edited by AirFlow
• 1
##### Share on other sites

Hi,

I can't see anything obviously wrong. The haversine calculation is giving correct values.

The only thing I can't check is the ground speed conversion:

`Dim speed As Double = ((gspeed.Value / 33714.6311111111) * 1.852)  ' User's Groundspeed`

Where is gspeed coming from? What units is it in?

Paul

## Create an account

Register a new account