Files
golifehk/query/match_locations.go
2026-03-10 15:18:34 +08:00

41 lines
739 B
Go

package query
import (
"fmt"
)
func MatchNearest(p IGeoLocation, entries *[]ISearchable, dist float64, fallback_limit int) (*QueryObject, error) {
terms := []*QTerm{
{
Org: fmt.Sprintf("%f, %f", p.Lat(), p.Lon()),
Value: "",
},
}
var locs *GeoLocations
locs = GeoLocations(*entries).Clean()
locs.SortByNearest(p)
matches := []ISearchable{}
for _, item := range *locs {
loc := item.(IGeoLocation)
if loc.Dist(p.Lat(), p.Lon()) <= dist {
matches = append(matches, item)
}
}
if len(matches) == 0 {
for i, item := range *locs {
if i < fallback_limit {
matches = append(matches, item)
} else {
break
}
}
}
return &QueryObject{Key: "", Results: &matches, SearchTerms: &terms}, nil
}