aboutsummaryrefslogtreecommitdiff
path: root/geo/geo.go
blob: 50ac4c78fcb97e6bc61de63e891c428a5891853e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 := range coords {
		distance := DistanceM(coords[index], target)
		if distance < bestDistance {
			bestIndex = index
			bestDistance = distance
		}
	}
	return bestIndex, bestDistance
}