From f5243503dbbe54ea61c602df3744c51004706315 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Thu, 27 Oct 2022 18:57:24 +0200 Subject: [PATCH] Add GeoDistance --- langext/coords.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/langext/coords.go b/langext/coords.go index 38ed6d7..ebeb830 100644 --- a/langext/coords.go +++ b/langext/coords.go @@ -9,3 +9,13 @@ func DegToRad(deg float64) float64 { func RadToDeg(rad float64) float64 { return rad / (math.Pi * 180.0) } + +func GeoDistance(lon1 float64, lat1 float64, lon2 float64, lat2 float64) float64 { + var d1 = DegToRad(lat1) + var num1 = DegToRad(lon1) + var d2 = DegToRad(lat2) + var num2 = DegToRad(lon2) - num1 + var d3 = math.Pow(math.Sin((d2-d1)/2.0), 2.0) + math.Cos(d1)*math.Cos(d2)*math.Pow(math.Sin(num2/2.0), 2.0) + + return 6376500.0 * (2.0 * math.Atan2(math.Sqrt(d3), math.Sqrt(1.0-d3))) +}