blob: 0fcff149cb1e8192ce4aae6b01e5b3e896b1a3a9 (
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 := 0; index < len(coords); index++ {
distance := DistanceM(coords[index], target)
if distance < bestDistance {
bestIndex = index
bestDistance = distance
}
}
return bestIndex, bestDistance
}
|