penguin/golifehk

@golifehk bot in Telegram

query/match_locations.go

raw ยท 739 bytes

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
}