diff --git a/cmd/maps.go b/cmd/maps.go index 0e7db25..6b7a54c 100644 --- a/cmd/maps.go +++ b/cmd/maps.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "log" + "math" "os" "path" @@ -48,6 +49,24 @@ func (m *Maps) textSearchOne(ctx context.Context, query string) (Location, error } func (m *Maps) Search(ctx context.Context, query string) ([]Location, error) { + results, err := m.search(ctx, query) + for i := len(results) - 1; i >= 0; i-- { + shouldKeep := true + if m.around != (Location{}) { + convertToMiles := 69.0 + a := m.around.Lat - results[i].Lat + b := m.around.Lng - results[i].Lng + dist := math.Sqrt(a*a + b*b) + shouldKeep = dist*convertToMiles < 30.0 + } + if !shouldKeep { + results = append(results[:i], results[i+1:]...) + } + } + return results, err +} + +func (m *Maps) search(ctx context.Context, query string) ([]Location, error) { cacheK := path.Join(fmt.Sprint(m.around), query) if b, err := m.cache.Get(ctx, cacheK); err == nil { var locations []Location