Get google place details for multiple place ids

How can I get google place details for multiple place ids? I used the following code to collect restaurant list and use the specific place id to get place details. Could someone help me on this

res <- google_places(search_string = "restaurants in Indiranagar, Bengaluru",
              radius = 500,
              key = key)

res

google_place_details(place_id = res$results$place_id[4], key = key)

for (i in 1:length(res$results$place_id)){
  res$results$details <- google_place_details(res$results$place_id[i], key = key)
}

Hi! Welcome to RStudio Community!

Is the code you are using not working or are you looking for a more efficient solution? If the code is not working please share the error message that you are getting.

From the documentation of the google_place_details function:

The amount of queries you can make to Google's APIs is dependent on both the service and the API you are using.

Each API has specific quotas and limits. Check Google's API documentation for details.

View your usage at the Google Cloud Console https://console.cloud.google.com/

Each API can only accept and return one request at a time. If you write a loop to make multiple API calls you should ensure you don't go over your quota / limits during the loop.

So if you are having issues with fulfilling all of your requests it is likely that it has to do with your allotted quota


In addition, it looks like your code was not formatted correctly to make it easy to read for people trying to help you. Formatting code allows for people to more easily identify where issues may be occurring, and makes it easier to read, in general. I have edited you post to format the code properly.

In the future please put code that is inline (such as a function name, like mutate or filter) inside of backticks (`mutate`) and chunks of code (including error messages and code copied from the console) can be put between sets of three backticks:

```
example <- foo %>%
  filter(a == 1)
```

This process can be done automatically by highlighting your code, either inline or in a chunk, and clicking the </> button on the toolbar of the reply window!

This will help keep our community tidy and help you get the help you are looking for!

For more information, please take a look at the community's FAQ on formating code

1 Like

Hi Bradley,

The code doesn't work when I try to run all place_ids using for loop. I get the following error

$html_attributions
list()

$status
[1] "INVALID_REQUEST"

It may be something to do with where you are placing the [i] in your for loop.

What is the output of the two following commands:

res$results$place_id[1]

and

res$results[1]$place_id

I am not familiar with this package so I don't know the structure of its output to be able to better tell you what could be causing this exact issue, but this is at least a place to start.

the commands you posted both give the entire value set for the first restaurant out of 20.
Although I changed the code slightly as follows,

library(googleway)

key <- 'xxxxxxxxxxxxxxxx'
devtools::install_github("SymbolixAU/googleway")



res <- google_places(search_string = "restaurants in Indiranagar, Bengaluru",
              radius = 500,
              key = key)

res
str(res)
str(res$results$place_id)

res$results$place_id <- as.vector(res$results$place_id)
j=0
for (i in res$results$place_id) {
  a[j] <- google_place_details(i, key = key)
  print(a)
  j+1
}

Problem I see is a prints out the same data of the first restaurant 20 times than printing all 20 restaurant data. Could you help me where I went wrong with the loop.

Hi Bradley,

I finally figured how to print all the data using for loop.

res <- google_places(search_string = "restaurants in Indiranagar, Bengaluru",
              radius = 500,
              key = key)

res
str(res)
str(res$results$place_id)

res$results$place_id <- as.vector(res$results$place_id)

for (i in 1:length(res$results$place_id)) {
  a <- google_place_details(res$results$place_id[i], key = key)
  print(a$result$name)
  print(a$result$rating)
  print(a$result$price_level)
  print(a$result$international_phone_number)
  print("#####################")
  }

Although I would like to know, how can I print a with all 20 restaurant data outside the loop.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.