Question : Calculate the distance in KM between two GPS Coordinates (Latitude and Longitude)

I am looking for a code in VB.NET,ASP. Net or MS SQL  to calculate the distance in KM between two GPS Coordinates (Latitude and Longitude). An example of the used GPS format is 25 14.997N 55 18.720E or N 25° 21' 44'' E 55° 23' 28''. Moreover, how the distance is calculated if each of the given point is using a different GPS format? Does it need conversion?

Thanks and regards


 

Answer : Calculate the distance in KM between two GPS Coordinates (Latitude and Longitude)

first you have to separate between parsing and calculation. Each coordinate format will need a different parsing routine. Besides this, you need to store coordinates always in the same format, let's say in degrees (and decimals).
About your original question (calculate distance), you can do it using the Haversine formula:



1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
Public Function DistanceKilometers(ByVal _lat1 As Double, ByVal _lon1 As Double, ByVal _lat2 As Double, ByVal _lon2 As Double) As Double
    Const rad As Double = Math.PI / 180
    
    Dim lon1 As Double = rad * -_lon1
    Dim lat1 As Double = rad * _lat1
    Dim lon2 As Double = rad * _lon2
    Dim lat2 As Double = rad * _lat2
    
    Dim d As Double = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin((lat1 - lat2) / 2), 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Pow(Math.Sin((lon1 - lon2) / 2), 2)))
    Return CSng((1.852 * 60 * d / rad))
End Function
Random Solutions  
 
programming4us programming4us