I have a requirement for determining the distance between a point (lat/long) a polyline on a map using the Mercator projection.
I have the following code (using the free Gmap.NET library), but it's not providing correct results, so I don't know if I've made a mistake somewhere and it just needs to be fixed, or if it needs to be completely rewritten.
Public Shared Function GetDistanceToLine(Point As GMap.NET.PointLatLng, LineStart As GMap.NET.PointLatLng, LineEnd As GMap.NET.PointLatLng) 'https://stackoverflow.com/questions/63739314/nearest-distance-between-one-marker-and-polyline-defined-by-two-ends
Dim lat1 As Double = LineStart.Lat
Dim lng1 As Double = LineStart.Lng
Dim lat2 As Double = LineEnd.Lat
Dim lng2 As Double = LineEnd.Lng
Dim lat3 As Double = Point.Lat
Dim lng3 As Double = Point.Lng
Dim R As Double = 6378137 'Earth mean radius
Dim slope As Double = (lng1 - lng2) / (lat1 - lat2)
Dim b As Double = (lng1) - (slope * lat1)
Dim y As Double = Math.Sin(lng3 - lng1) * Math.Cos(lat3)
Dim x As Double = Math.Cos(lat1) * Math.Sin(lat3) - Math.Sin(lat1) * Math.Cos(lat3) * Math.Cos(lat3 - lat1)
Dim y2 As Double = Math.Sin(lng2 - lng1) * Math.Cos(lat2)
Dim x2 As Double = Math.Cos(lat1) * Math.Sin(lat2) - Math.Sin(lat1) * Math.Cos(lat2) * Math.Cos(lat2 - lat1)
Dim bearing1 As Double = Degrees(Math.Atan2(y, x))
bearing1 = 360 - ((bearing1 + 360) Mod 360)
Dim bearing2 As Double = Degrees(Math.Atan2(y2, x2))
bearing2 = 360 - ((bearing2 + 360) Mod 360)
Dim lat1Rads As Double = Radians(lat1)
Dim lat3Rads As Double = Radians(lat3)
Dim dLon As Double = Radians(lng3 - lng1)
Dim distanceAC As Double = Math.Acos(Math.Sin(lat1Rads) * Math.Sin(lat3Rads) + Math.Cos(lat1Rads) * Math.Cos(lat3Rads) * Math.Cos(dLon)) * R
'=======================
Return Math.Abs(Math.Asin(Math.Sin(distanceAC / R) * Math.Sin(Radians(bearing1) - Radians(bearing2))) * R)
End Function
Hourly Range: $20.00-$40.00
Posted On: March 01, 2023 05:14 UTC
Category: Desktop Software Development
Skills:C#, .NET Framework, GIS, Algorithm Development
Country: Australia
click to apply
I have the following code (using the free Gmap.NET library), but it's not providing correct results, so I don't know if I've made a mistake somewhere and it just needs to be fixed, or if it needs to be completely rewritten.
Public Shared Function GetDistanceToLine(Point As GMap.NET.PointLatLng, LineStart As GMap.NET.PointLatLng, LineEnd As GMap.NET.PointLatLng) 'https://stackoverflow.com/questions/63739314/nearest-distance-between-one-marker-and-polyline-defined-by-two-ends
Dim lat1 As Double = LineStart.Lat
Dim lng1 As Double = LineStart.Lng
Dim lat2 As Double = LineEnd.Lat
Dim lng2 As Double = LineEnd.Lng
Dim lat3 As Double = Point.Lat
Dim lng3 As Double = Point.Lng
Dim R As Double = 6378137 'Earth mean radius
Dim slope As Double = (lng1 - lng2) / (lat1 - lat2)
Dim b As Double = (lng1) - (slope * lat1)
Dim y As Double = Math.Sin(lng3 - lng1) * Math.Cos(lat3)
Dim x As Double = Math.Cos(lat1) * Math.Sin(lat3) - Math.Sin(lat1) * Math.Cos(lat3) * Math.Cos(lat3 - lat1)
Dim y2 As Double = Math.Sin(lng2 - lng1) * Math.Cos(lat2)
Dim x2 As Double = Math.Cos(lat1) * Math.Sin(lat2) - Math.Sin(lat1) * Math.Cos(lat2) * Math.Cos(lat2 - lat1)
Dim bearing1 As Double = Degrees(Math.Atan2(y, x))
bearing1 = 360 - ((bearing1 + 360) Mod 360)
Dim bearing2 As Double = Degrees(Math.Atan2(y2, x2))
bearing2 = 360 - ((bearing2 + 360) Mod 360)
Dim lat1Rads As Double = Radians(lat1)
Dim lat3Rads As Double = Radians(lat3)
Dim dLon As Double = Radians(lng3 - lng1)
Dim distanceAC As Double = Math.Acos(Math.Sin(lat1Rads) * Math.Sin(lat3Rads) + Math.Cos(lat1Rads) * Math.Cos(lat3Rads) * Math.Cos(dLon)) * R
'=======================
Return Math.Abs(Math.Asin(Math.Sin(distanceAC / R) * Math.Sin(Radians(bearing1) - Radians(bearing2))) * R)
End Function
Hourly Range: $20.00-$40.00
Posted On: March 01, 2023 05:14 UTC
Category: Desktop Software Development
Skills:C#, .NET Framework, GIS, Algorithm Development
Country: Australia
click to apply