Exemplo n.º 1
0
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)

	resp, err := client.Geocode(context.Background(), r)
	check(err)

	pretty.Println(resp)
}
Exemplo n.º 2
0
func geocode(mapsClient *maps.Client, lat float64, lng float64) (string, string) {
	r := &maps.GeocodingRequest{
		LatLng: &maps.LatLng{Lat: lat, Lng: lng},
	}

	resp, err := mapsClient.Geocode(context.Background(), r)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	var city, country string
	for _, b := range resp[0].AddressComponents {
		if stringInSlice("country", b.Types) {
			country = b.LongName
		}
		if stringInSlice("locality", b.Types) {
			city = b.LongName
		}
	}

	return city, country
}