-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeo.go
59 lines (50 loc) · 1.1 KB
/
geo.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
type Location struct {
Title string `json:"title"`
ID string `json:"id"`
ResultType string `json:"resultType"`
Position struct {
Lat float32
Lng float32
}
}
func Geocode(address string) (*Location, error) {
if len(hereToken) == 0 {
return nil, fmt.Errorf("here token not found")
}
req, err := http.NewRequest("GET", "https://geocode.search.hereapi.com/v1/geocode", nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("q", address)
q.Add("apiKey", hereToken)
req.URL.RawQuery = q.Encode()
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
log.Println(resp.StatusCode)
if resp.StatusCode != 200 {
return nil, fmt.Errorf("Geocode non 200 return code %d", resp.StatusCode)
}
out, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
output := struct {
Items []Location
}{}
err = json.Unmarshal(out, &output)
if err != nil {
return nil, err
}
return &output.Items[0], nil
}