aboutsummaryrefslogtreecommitdiff
path: root/geo/geo.go
diff options
context:
space:
mode:
Diffstat (limited to 'geo/geo.go')
-rw-r--r--geo/geo.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/geo/geo.go b/geo/geo.go
new file mode 100644
index 0000000..0fcff14
--- /dev/null
+++ b/geo/geo.go
@@ -0,0 +1,25 @@
+package geo
+
+import (
+ "math"
+
+ "github.com/jftuga/geodist"
+)
+
+func DistanceM(a, b geodist.Coord) float64 {
+ _, distanceKm := geodist.HaversineDistance(a, b)
+ return distanceKm * 1000
+}
+
+func Nearest(coords []geodist.Coord, target geodist.Coord) (int, float64) {
+ bestIndex := 0
+ bestDistance := math.Inf(1)
+ for index := 0; index < len(coords); index++ {
+ distance := DistanceM(coords[index], target)
+ if distance < bestDistance {
+ bestIndex = index
+ bestDistance = distance
+ }
+ }
+ return bestIndex, bestDistance
+}