-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (49 loc) · 1.16 KB
/
main.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
package amap_coordinate
import (
"encoding/json"
"fmt"
"github.com/davecgh/go-spew/spew"
"net/http"
"net/url"
"strings"
"time"
)
func NewClient (apiKey string) *SearchClient {
return &SearchClient{
client: &http.Client{
Timeout: time.Minute,
},
apiKey: apiKey,
}
}
func (c *SearchClient) Search(name string, types string) (SearchPOIResult, error) {
u := url.Values{}
u.Add("key", c.apiKey)
u.Add("keywords", name)
u.Add("types", types)
uu := fmt.Sprintf("https://restapi.amap.com/v3/place/text?%s", u.Encode())
spew.Dump(uu)
response, err := http.Get(uu)
if err != nil {
return SearchPOIResult{}, err
}
if response.StatusCode != 200 {
spew.Dump(response.Body)
return SearchPOIResult{}, fmt.Errorf("not 200 status code: %v, %v", response.StatusCode, response)
}
var r SearchResponse
err = json.NewDecoder(response.Body).Decode(&r)
if err != nil {
return SearchPOIResult{}, err
}
spew.Dump(r)
if len(r.POIs) < 1 {
return SearchPOIResult{}, fmt.Errorf("noresult")
}
location := r.POIs[0].Location
segments := strings.Split(location, ",")
return SearchPOIResult{
Latitude: segments[0],
Longitude: segments[1],
}, nil
}