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("
")
// 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("
Name: ")
listings.WriteString(res.Results[i].Name)
listings.WriteString("
Address: ")
listings.WriteString(res.Results[i].Vicinity)
listings.WriteString("
Place ID: ")
listings.WriteString(res.Results[i].PlaceID)
listings.WriteString("
---------------------------------------------
")
}
listings.WriteString("
pagetoken is now:
")
listings.WriteString(res.NextPageToken)
if err != nil {
log.Fatal(err)
}
fmt.Println(listings.String())
fmt.Printf("
")
// LOOP BACK THROUGH FUNCTION
searchPlaces(res.NextPageToken)
}
func main() {
searchPlaces("")
}