2

I am looking for all starbucks' stores in a given location through googleway's google_places function.

library(googleway)
res <- google_places(search_string = 'starbucks in Sao Paulo SP',
                       key = my_key)
# get second page of results
token <- res$next_page_token
res_next1   <- google_places(search_string = 'starbucks in Sao Paulo SP',
                         page_token    = token,
                         key = my_key)

This works fine, however, when I attempt to get the next 20 results (page 3 of results) I get the following error

token2<- res_next1$next_page_token
res_next2 <- google_places(search_string = 'starbucks in Sao Paulo SP',
                     page_token    = token2,
                     key = my_key)
Error: lexical error: invalid char in json text.
                                   https://maps.googleapis.com/map
                 (right here) ------^

This problem was solved with the new github version of the package.

A subsequent problem was when trying to access all page results in a loop

lool = list()
# problem happens when searches[i] = 'starbucks in Sao Paulo, Sao Paulo'
for(i in 1:length(searches)){

  j = 1 # page counter
  res <- google_places(search_string = searches[i],
                       key = key)

  if(is.null(res$results$name)) {cat(i, 'null\n'); next}

  lool[[paste0(i, '-', j)]] <- data.frame(title   = res$results$name,
                                          address = res$results$formatted_address)
  token = res$next_page_token

  while(!is.null(token)){
    j = j + 1
    res_n <- google_places(search_string = searches[i],
                           page_token    = token,
                           key = key)

    lool[[paste0(i, '-', j)]] <- data.frame(title   = res_n$results$name,
                                            address = res_n$results$formatted_address)
    token <- res_n$next_page_token

  }
  aa = res$status
  cat(i, j, aa, '\n')
}

With this code, the third page results in

$`html_attributions`
list()

$`results`
list()

$`status`
[1] "INVALID_REQUEST"

While if I run the code like this

token <- res$next_page_token

  if(!is.null(token)){
    j     <- j + 1
    res_2 <- google_places(search_string = searches[i],
                           page_token    = token,
                           key = joao_key)

    lool[[paste0(i, '-', j)]] <- data.frame(title   = res_2$results$name,
                                            address = res_2$results$formatted_address)
    token2 <- res_2$next_page_token
    #print(j)
  }
  if(!is.null(token2)){
    j = j + 1
    res_3 <- google_places(search_string = searches[i],
                           page_token    = token,
                           key = joao_key)

    lool[[paste0(i, '-', j)]] <- data.frame(title   = res_3$results$name,
                                            address = res_3$results$formatted_address)
    token3 <- res_3$next_page_token
    #print(j)
  }
  if(!is.null(token3)){
    j = j + 1
    res_4 <- google_places(search_string = searches[i],
                           page_token    = token,
                           key = joao_key)

    lool[[paste0(i, '-', j)]] <- data.frame(title   = res_4$results$name,
                                            address = res_4$results$formatted_address)
    token4 <- res_4$next_page_token
    #print(j)
  }

all works fine. What am I missing here?

Felipe Alvarenga
  • 2,572
  • 1
  • 17
  • 36
  • Please see [this issue](https://github.com/SymbolixAU/googleway/issues/93#issuecomment-385562285). The [development version](https://github.com/SymbolixAU/googleway) has a fix. You can install it with `devtools::install_github("SymbolixAU/googleway")` – SymbolixAU May 23 '18 at 21:49
  • I got it to work, but then I found a different problem. When I try to run something like `while(!is.null(res$next_page_token){res_next1 ...}` the second request goes well, but the following return an invalid request – Felipe Alvarenga May 24 '18 at 13:56
  • it does work, however, if I copy paste the code an infinite amount of times, always creating a new `res_next_page = google_places(..., page_token = token_page)`. I have updated the question to show the entire code – Felipe Alvarenga May 24 '18 at 13:57
  • With your updated code (the `while` loop) I don't have any issues - it returns 3 pages of results. Perhaps you're hitting [your query limit](https://developers.google.com/places/web-service/usage), or you're sending to many queries too close together? – SymbolixAU May 24 '18 at 21:53
  • 1
    The problem was the second option. I had to to put a `Sys.Sleep(1.5)` for each iteration, otherwise the request would be invalid – Felipe Alvarenga May 25 '18 at 13:51
  • Can you post a working solution for this. Whereever I try to put sleep time, it is still not working @FelipeAlvarenga – Marco Jan 02 '23 at 13:16

0 Answers0