-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreverse.go
88 lines (73 loc) · 1.9 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package geocodio
import (
"fmt"
"strconv"
)
/*
See: http://geocod.io/docs/#toc_16
*/
// Reverse does a reverse geocode look up for a single coordinate
func (g *Geocodio) Reverse(latitude, longitude float64) (GeocodeResult, error) {
// if there is an address here, they should probably think about moving
// regardless, we'll consider it an error
if latitude == 0.0 && longitude == 0.0 {
return GeocodeResult{}, ErrReverseGecodeMissingLatLng
}
latStr := strconv.FormatFloat(latitude, 'f', 9, 64)
lngStr := strconv.FormatFloat(longitude, 'f', 9, 64)
resp := GeocodeResult{}
err := g.get("/reverse", map[string]string{"q": latStr + "," + lngStr}, &resp)
if err != nil {
return resp, err
}
if len(resp.Results) == 0 {
return resp, ErrNoResultsFound
}
return resp, nil
}
// ReverseGeocode is deprecated and will be removed in 2+
func (g *Geocodio) ReverseGeocode(latitude, longitude float64) (GeocodeResult, error) {
fmt.Printf(`
ReverseGeocode(%f, %f) is deprecated and will be removed in 2+
Use Reverse(%f, %f)
`,
latitude, longitude,
latitude, longitude,
)
return g.Reverse(latitude, longitude)
}
// ReverseBatch supports a batch lookup by lat/lng coordinate pairs
func (g *Geocodio) ReverseBatch(latlngs ...float64) (BatchResponse, error) {
resp := BatchResponse{}
if len(latlngs) == 0 {
return resp, ErrReverseBatchMissingCoords
}
if len(latlngs)%2 == 1 {
return resp, ErrReverseBatchInvalidCoordsPairs
}
var (
payload = []string{}
pair string
)
for i := range latlngs {
coord := strconv.FormatFloat(latlngs[i], 'f', 9, 64)
if i == 0 {
pair = coord
continue
}
if i%2 == 0 {
pair = fmt.Sprintf("%s,%s", pair, coord)
payload = append(payload, pair)
continue
}
pair = coord
}
err := g.post("/reverse", payload, nil, &resp)
if err != nil {
return resp, err
}
if len(resp.Results) == 0 {
return resp, ErrNoResultsFound
}
return resp, nil
}