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()
xmlDoc.Load(xmlFile)
' 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
' Convert degrees to radians
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 dLat As Double = endLatRad - startLatRad
Dim dLon As Double = endLonRad - startLonRad
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?