filter to 30mi radius

This commit is contained in:
bel
2026-03-09 00:06:43 -06:00
parent 2662e056e0
commit 55a78add6f

View File

@@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"math"
"os" "os"
"path" "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) { 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) cacheK := path.Join(fmt.Sprint(m.around), query)
if b, err := m.cache.Get(ctx, cacheK); err == nil { if b, err := m.cache.Get(ctx, cacheK); err == nil {
var locations []Location var locations []Location