コード例 #1
0
ファイル: main.go プロジェクト: zeroed/alphazerobot
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)
}
コード例 #2
0
ファイル: scrape_wayfinding.go プロジェクト: d4l3k/campus
func geocodeBuildings(c *maps.Client) error {
	buildings, err := models.LoadMapData()
	if err != nil {
		return err
	}
	for _, b := range buildings {
		if b.Position != nil || len(b.Address) == 0 {
			continue
		}
		log.Printf("geocoding %s", b.Name)
		req := &maps.GeocodingRequest{
			Address: b.Address,
			Region:  "ca",
		}
		result, err := c.Geocode(context.TODO(), req)
		if err != nil {
			return err
		}
		if len(result) == 0 {
			continue
		}
		loc := result[0].Geometry.Location
		b.Position = &models.LatLng{
			Lat: loc.Lat,
			Lng: loc.Lng,
		}
		time.Sleep(100 * time.Millisecond)
		if err := models.SaveMapData(buildings); err != nil {
			return err
		}
	}
	for _, b := range buildings {
		if len(b.Floors) == 0 {
			continue
		}
		var lat, lng float64
		c := float64(len(b.Floors) * 2)
		for _, f := range b.Floors {
			lat += f.Coords.North
			lat += f.Coords.South
			lng += f.Coords.West
			lng += f.Coords.East
			for _, r := range f.Rooms {
				lat += r.Position.Lat
				lng += r.Position.Lng
				c += 1
			}
		}
		b.Position.Lat = lat / c
		b.Position.Lng = lng / c
	}
	return models.SaveMapData(buildings)
}