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

在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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料