-
Notifications
You must be signed in to change notification settings - Fork 0
/
geocoder.go
115 lines (100 loc) · 2.52 KB
/
geocoder.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package geocoder
/* Initialize with a key from https://developers.google.com/maps/documentation/geocoding/get-api-key */
// geo := Geocoder{key: "......"}
/* This will return a JSON reponse */
// json := geo.GetJSON("Tokyo, Japan")
/* This will return a Response struct as seen below */
// response := geo.DecodeJSON("zipcode M2H 2G6")
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
// Geocoder struct
type Geocoder struct {
url string
key string
}
// Response json
type Response struct {
Results []Result `json:"results"`
Status string `json:"status"`
}
// Result json
type Result struct {
AddressComponents []Component `json:"address_components"`
FormattedAddress string `json:"formatted_address"`
Geometry Geometry
PartialMatch bool `json:"partial_match"`
PlaceID string `json:"place_id"`
Types []string `json:"types"`
}
// Component json
type Component struct {
LongName string `json:"long_name"`
ShortName string `json:"short_name"`
Types []string
}
// Geometry json
type Geometry struct {
Bounds Square `json:"bounds"`
Location Coordinate `json:"location"`
LocationType string `json:"location_type"`
Viewport Square `json:"viewport"`
}
// Square json
type Square struct {
Northeast Coordinate `json:"northeast"`
Southwest Coordinate `json:"southwest"`
}
// Coordinate json
type Coordinate struct {
Lat string `json:"lat"`
Lng string `json:"lng"`
}
func (g *Geocoder) setURL(geo string) {
geo = strings.Replace(geo, " ", "+", -1)
g.url = fmt.Sprintf("https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=%s", geo, g.key)
}
// DecodeJSON returns the Google result as a struct
func (g *Geocoder) DecodeJSON(geo string) Response {
g.setURL(geo)
response := new(Response)
r, err := http.Get(g.url)
if err != nil {
panic(err)
}
defer r.Body.Close()
err = json.NewDecoder(r.Body).Decode(response)
if err != nil {
panic(err)
}
return *response
}
// GetJSON returs the Google result as a JSON string
func (g *Geocoder) GetJSON(geo string) string {
g.setURL(geo)
fmt.Println(geo)
fmt.Println(g.url)
r, err := http.Get(g.url)
if err != nil {
panic(err)
}
defer r.Body.Close()
contents, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
return string(contents)
}
// GetComponent fetches an item by name from the matched places
func GetComponent(components []Component, name string) string {
for _, c := range components {
if c.Types[0] == name {
return c.LongName
}
}
return ""
}