-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse.go
54 lines (44 loc) 路 1.11 KB
/
reverse.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
package loctools
import (
"errors"
"fmt"
"io"
"net/url"
geojson "github.com/paulmach/go.geojson"
)
func (l *LocTools) photonReverse(lang string, lon, lat float64) (*Location, error) {
uv := url.Values{}
uv.Set("lat", fmt.Sprintf("%v", lat))
uv.Set("lon", fmt.Sprintf("%v", lon))
uv.Set("lang", lang)
res, err := l.httpClient.Get("https://photon.komoot.io/reverse?" + 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")
}
return &Location{
Latitude: lat,
Longitude: lon,
Locality: city,
Region: state,
Country: country,
}, nil
}