dongtuanzi1080
2017-02-02 04:38
浏览 132
已采纳

在Go中使用pagetoken遍历Google Places API的麻烦

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("")
}
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dryeyhe0266 2017-02-02 12:19
    已采纳

    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.

    打赏 评论

相关推荐 更多相似问题