Reputation: 467
Im having trouble looping through the Google Places API in Go.
Google's Places API returns 20 results max with a pagetoken parameter to add to the query to return the next 20 results until theres none left.
I currently am able to send a query request, return the json and output it in terminal, but when i try to loop back through and add the pagetoken
parameter to the query, it runs but only returns the first page results again but with another page token. Any Idea what im doing wrong?
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
// "os"
)
type GooglePlaces struct {
HTMLAttributions []interface{} `json:"html_attributions"`
NextPageToken string `json:"next_page_token"`
Results []struct {
Geometry struct {
Location struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"location"`
Viewport struct {
Northeast struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"northeast"`
Southwest struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"southwest"`
} `json:"viewport"`
} `json:"geometry"`
Icon string `json:"icon"`
ID string `json:"id"`
Name string `json:"name"`
OpeningHours struct {
OpenNow bool `json:"open_now"`
WeekdayText []interface{} `json:"weekday_text"`
} `json:"opening_hours,omitempty"`
Photos []struct {
Height int `json:"height"`
HTMLAttributions []string `json:"html_attributions"`
PhotoReference string `json:"photo_reference"`
Width int `json:"width"`
} `json:"photos,omitempty"`
PlaceID string `json:"place_id"`
Reference string `json:"reference"`
Scope string `json:"scope"`
Types []string `json:"types"`
Vicinity string `json:"vicinity"`
Rating float64 `json:"rating,omitempty"`
} `json:"results"`
Status string `json:"status"`
}
func searchPlaces(page string) {
apiKey := "API_KEY_HERE"
keyword := "residential+bank+33131"
latLong := "25.766144,-80.190589"
pageToken := page
var buffer bytes.Buffer
buffer.WriteString("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=")
buffer.WriteString(latLong)
buffer.WriteString("&radius=50000&keyword=")
buffer.WriteString(keyword)
buffer.WriteString("&key=")
buffer.WriteString(apiKey)
buffer.WriteString("&pagetoken=")
buffer.WriteString(pageToken)
query := buffer.String()
// PRINT CURRENT SEARCH
println("query is ", query)
println("\n")
// SEND REQUEST WITH QUERY
resp, err := http.Get(query)
if err != nil {
log.Fatal(err)
}
// CLOSE THE PRECLOSER THATS RETURNED WITH HTTP RESPONSE
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
res := GooglePlaces{}
json.Unmarshal([]byte(body), &res)
var listings bytes.Buffer
for i := 0; i < len(res.Results); i++ {
listings.WriteString(strconv.Itoa(i + 1))
listings.WriteString("\nName: ")
listings.WriteString(res.Results[i].Name)
listings.WriteString("\nAddress: ")
listings.WriteString(res.Results[i].Vicinity)
listings.WriteString("\nPlace ID: ")
listings.WriteString(res.Results[i].PlaceID)
listings.WriteString("\n---------------------------------------------\n\n")
}
listings.WriteString("\npagetoken is now:\n")
listings.WriteString(res.NextPageToken)
if err != nil {
log.Fatal(err)
}
fmt.Println(listings.String())
fmt.Printf("\n\n\n")
// LOOP BACK THROUGH FUNCTION
searchPlaces(res.NextPageToken)
}
func main() {
searchPlaces("")
}
Upvotes: 0
Views: 1335
Reputation: 5500
Note that in the documentation for Google Place Search they state:
There is a short delay between when a next_page_token is issued, and when it will become valid.
But in your code you immediately send a request with the new token.
Adding a sleep for a few seconds before using the token solves the problem for me. I changed your code to
if res.NextPageToken != "" {
time.Sleep(3000 * time.Millisecond)
searchPlaces(res.NextPageToken)
} else {
fmt.Println("No more pagetoken, we're done.")
}
Unfortunately there's no documentation about for how long a token is valid.
Upvotes: 2