-
Notifications
You must be signed in to change notification settings - Fork 3
/
searchbox.go
91 lines (76 loc) · 2.83 KB
/
searchbox.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
89
90
91
package mapbox
import (
"context"
"fmt"
"net/url"
"strconv"
)
const (
SearchboxReverseEndpoint = "/search/searchbox/v1/reverse"
)
type SearchboxReverseRequest struct {
Coordinate
// optional
Country string `json:"country,omitempty"`
Language string `json:"language,omitempty"`
Limit int `json:"limit,omitempty"`
Types Types `json:"types,omitempty"`
}
type SearchboxReverseResponse struct {
Type string `json:"type"`
Features []*SearchboxReverseFeature `json:"features"`
Attribution string `json:"attribution"`
}
type SearchboxReverseFeature struct {
ID string `json:"id"`
Type string `json:"type"`
Geometry *Geometry `json:"geometry"`
Properties *SearchboxReverseProperties `json:"properties,omitempty"`
}
type SearchboxReverseProperties struct {
MapboxID string `json:"mapbox_id"`
FeatureType Type `json:"feature_type"`
Name string `json:"name"`
NamePreferred string `json:"name_preferred"`
PlaceFormatted string `json:"place_formatted"`
FullAddress string `json:"full_address"`
Coordinates ExtendedCoordinate `json:"coordinates"`
Context map[Type]Context `json:"context,omitempty"`
BoundingBox []float64 `json:"bbox,omitempty"`
Language string `json:"language"`
Maki string `json:"maki"`
POICategory []string `json:"poi_category"`
POICategoryIDs []string `json:"poi_category_ids"`
Brand []string `json:"brand"`
BrandID []string `json:"brand_id"`
ExternalIDs map[string]string `json:"external_ids,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
// https://docs.mapbox.com/api/search/search-box/#reverse-lookup
func searchboxReverse(ctx context.Context, client *Client, req *SearchboxReverseRequest) (*SearchboxReverseResponse, error) {
query := url.Values{}
query.Set("access_token", client.apiKey)
query.Set("latitude", strconv.FormatFloat(req.Lat, 'f', -1, 64))
query.Set("longitude", strconv.FormatFloat(req.Lng, 'f', -1, 64))
if req.Country != "" {
query.Set("country", req.Country)
}
if req.Language != "" {
query.Set("language", req.Language)
}
if req.Limit > 0 {
query.Set("limit", fmt.Sprintf("%v", req.Limit))
}
if len(req.Types) > 0 {
query.Set("types", req.Types.query())
}
apiResponse, err := client.get(ctx, SearchboxReverseEndpoint, query)
if err != nil {
return nil, err
}
var response SearchboxReverseResponse
if err := client.handleResponse(apiResponse, &response, SearchboxRateLimit); err != nil {
return nil, err
}
return &response, nil
}