can search
This commit is contained in:
59
cmd/main.go
59
cmd/main.go
@@ -2,14 +2,21 @@ package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"golang.org/x/time/rate"
|
||||
"googlemaps.github.io/maps"
|
||||
)
|
||||
|
||||
func Main(ctx context.Context) error {
|
||||
func Run(ctx context.Context) error {
|
||||
rps := 2
|
||||
limit := 100
|
||||
|
||||
limiter := rate.NewLimiter(rate.Limit(rps), 1)
|
||||
|
||||
c, err := maps.NewClient(maps.WithAPIKey(os.Getenv("GOOGLE_PLACES_API_KEY")))
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -22,23 +29,41 @@ func Main(ctx context.Context) error {
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
} else if len(resp.Results) < 1 {
|
||||
return fmt.Errorf("no results for %q", os.Args[1])
|
||||
}
|
||||
return fmt.Errorf("%+v", resp)
|
||||
origin := resp.Results[0].Geometry.Location
|
||||
|
||||
/*
|
||||
resp, err := c.NearbySearch(ctx, &maps.NearbySearchRequest{
|
||||
Location: &maps.LatLng{},
|
||||
Radius: uint(0),
|
||||
Keyword: os.Args[1],
|
||||
Name: "",
|
||||
OpenNow: false,
|
||||
type Result struct {
|
||||
Name string
|
||||
Lat float64
|
||||
Lng float64
|
||||
}
|
||||
results := []Result{}
|
||||
var nextToken string
|
||||
for len(results) < limit {
|
||||
limiter.Wait(ctx)
|
||||
resp, err := c.TextSearch(ctx, &maps.TextSearchRequest{
|
||||
Query: os.Args[2],
|
||||
Location: &origin,
|
||||
Radius: uint(50),
|
||||
PageToken: nextToken,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nextToken = resp.NextPageToken
|
||||
for _, result := range resp.Results {
|
||||
results = append(results, Result{
|
||||
Name: result.Name,
|
||||
Lat: result.Geometry.Location.Lat,
|
||||
Lng: result.Geometry.Location.Lng,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
log.Printf("%d...", len(resp.Results))
|
||||
}
|
||||
b, _ := json.Marshal(results)
|
||||
fmt.Printf("%s\n", b)
|
||||
|
||||
return fmt.Errorf("%+v", resp)
|
||||
*/
|
||||
|
||||
return io.EOF
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user