-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
58 lines (46 loc) 路 1.15 KB
/
search.go
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package loctools
import (
"errors"
"io"
"net/url"
geojson "github.com/paulmach/go.geojson"
)
func (l *LocTools) photonSearch(lang, query string) (*Location, error) {
uv := url.Values{}
uv.Set("q", query)
uv.Set("lang", lang)
res, err := l.httpClient.Get("https://photon.komoot.io/api/?" + uv.Encode())
if err != nil {
return nil, err
}
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
fc, err := geojson.UnmarshalFeatureCollection(data)
if err != nil {
return nil, err
}
if len(fc.Features) < 1 {
return nil, errors.New("features missing from request")
}
f := fc.Features[0]
city := f.PropertyMustString("city", "")
state := f.PropertyMustString("state", f.PropertyMustString("county", ""))
country := f.PropertyMustString("country", "")
if city == "" && state == "" && country == "" {
return nil, errors.New("no useful information found")
}
loc := &Location{
Name: query,
Locality: city,
Region: state,
Country: country,
}
if f.Geometry != nil && len(f.Geometry.Point) == 2 {
loc.Longitude = f.Geometry.Point[0]
loc.Latitude = f.Geometry.Point[1]
}
return loc, nil
}