-
Notifications
You must be signed in to change notification settings - Fork 214
/
main.go
199 lines (177 loc) · 6 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package main contains a simple command line tool for Geocoding API
// Documentation: https://developers.google.com/maps/documentation/geocoding/
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/kr/pretty"
"googlemaps.github.io/maps"
)
var (
apiKey = flag.String("key", "", "API Key for using Google Maps API.")
clientID = flag.String("client_id", "", "ClientID for Maps for Work API access.")
signature = flag.String("signature", "", "Signature for Maps for Work API access.")
address = flag.String("address", "", "The street address that you want to geocode, in the format used by the national postal service of the country concerned.")
components = flag.String("components", "", "A component filter for which you wish to obtain a geocode.")
bounds = flag.String("bounds", "", "The bounding box of the viewport within which to bias geocode results more prominently.")
language = flag.String("language", "", "The language in which to return results.")
region = flag.String("region", "", "The region code, specified as a ccTLD two-character value.")
latlng = flag.String("latlng", "", "The textual latitude/longitude value for which you wish to obtain the closest, human-readable address.")
resultType = flag.String("result_type", "", "One or more address types, separated by a pipe (|).")
locationType = flag.String("location_type", "", "One or more location types, separated by a pipe (|).")
enableAddressDescriptor = flag.String("enable_address_descriptor", "", "True or False. Whether to return the Address Descriptors in the response.")
)
func usageAndExit(msg string) {
fmt.Fprintln(os.Stderr, msg)
fmt.Println("Flags:")
flag.PrintDefaults()
os.Exit(2)
}
func check(err error) {
if err != nil {
log.Fatalf("fatal error: %s", err)
}
}
func main() {
flag.Parse()
var client *maps.Client
var err error
if *apiKey != "" {
client, err = maps.NewClient(maps.WithAPIKey(*apiKey))
} else if *clientID != "" || *signature != "" {
client, err = maps.NewClient(maps.WithClientIDAndSignature(*clientID, *signature))
} else {
usageAndExit("Please specify an API Key, or Client ID and Signature.")
}
check(err)
r := &maps.GeocodingRequest{
Address: *address,
Language: *language,
Region: *region,
}
parseComponents(*components, r)
parseBounds(*bounds, r)
parseLatLng(*latlng, r)
parseResultType(*resultType, r)
parseLocationType(*locationType, r)
parseEnableAddressDescriptor(*enableAddressDescriptor, r)
resp, err := client.Geocode(context.Background(), r)
check(err)
pretty.Println(resp)
}
func parseComponents(components string, r *maps.GeocodingRequest) {
if components == "" {
return
}
if r.Components == nil {
r.Components = make(map[maps.Component]string)
}
c := strings.Split(components, "|")
for _, cf := range c {
i := strings.Split(cf, ":")
switch i[0] {
case "route":
r.Components[maps.ComponentRoute] = i[1]
case "locality":
r.Components[maps.ComponentLocality] = i[1]
case "administrative_area":
r.Components[maps.ComponentAdministrativeArea] = i[1]
case "postal_code":
r.Components[maps.ComponentPostalCode] = i[1]
case "country":
r.Components[maps.ComponentCountry] = i[1]
default:
log.Fatalf("parseComponents: component name %#v unknown", i[0])
}
}
}
func parseBounds(bounds string, r *maps.GeocodingRequest) {
if bounds != "" {
b := strings.Split(bounds, "|")
sw := strings.Split(b[0], ",")
ne := strings.Split(b[1], ",")
swLat, err := strconv.ParseFloat(sw[0], 64)
if err != nil {
log.Fatalf("Couldn't parse bounds: %#v", err)
}
swLng, err := strconv.ParseFloat(sw[1], 64)
if err != nil {
log.Fatalf("Couldn't parse bounds: %#v", err)
}
neLat, err := strconv.ParseFloat(ne[0], 64)
if err != nil {
log.Fatalf("Couldn't parse bounds: %#v", err)
}
neLng, err := strconv.ParseFloat(ne[1], 64)
if err != nil {
log.Fatalf("Couldn't parse bounds: %#v", err)
}
r.Bounds = &maps.LatLngBounds{
NorthEast: maps.LatLng{Lat: neLat, Lng: neLng},
SouthWest: maps.LatLng{Lat: swLat, Lng: swLng},
}
}
}
func parseLatLng(latlng string, r *maps.GeocodingRequest) {
if latlng != "" {
l := strings.Split(latlng, ",")
lat, err := strconv.ParseFloat(l[0], 64)
if err != nil {
log.Fatalf("Couldn't parse latlng: %#v", err)
}
lng, err := strconv.ParseFloat(l[1], 64)
if err != nil {
log.Fatalf("Couldn't parse latlng: %#v", err)
}
r.LatLng = &maps.LatLng{
Lat: lat,
Lng: lng,
}
}
}
func parseResultType(resultType string, r *maps.GeocodingRequest) {
if resultType != "" {
r.ResultType = strings.Split(resultType, "|")
}
}
func parseLocationType(locationType string, r *maps.GeocodingRequest) {
if locationType != "" {
for _, l := range strings.Split(locationType, "|") {
switch l {
case "ROOFTOP":
r.LocationType = append(r.LocationType, maps.GeocodeAccuracyRooftop)
case "RANGE_INTERPOLATED":
r.LocationType = append(r.LocationType, maps.GeocodeAccuracyRangeInterpolated)
case "GEOMETRIC_CENTER":
r.LocationType = append(r.LocationType, maps.GeocodeAccuracyGeometricCenter)
case "APPROXIMATE":
r.LocationType = append(r.LocationType, maps.GeocodeAccuracyApproximate)
}
}
}
}
func parseEnableAddressDescriptor(enableAddressDescriptor string, r *maps.GeocodingRequest) {
if enableAddressDescriptor == "True" {
r.EnableAddressDescriptor = true
} else {
r.EnableAddressDescriptor = false
}
}