Bdude11383 Bdude11383 - 2 years ago 155
JSON Question

Multiple Google Places API calls within Sapply function

I have a list of locations that I'm feeding into the Google Places API. Some locations have more than 20 results. I'm providing an example of one such location below. To get results beyond the first 20, you have to make an additional API call to Google Places, with an extra "token" parameter that is obtained from the first Google Places API call.

Using the below flawed function, I'm attempting to execute the additional API call, based on whether there are additional results that need to be obtained. The current function produces NULL values. Any help on correcting this function would be highly appreciated.

List to Feed into Sapply:

LatLongList <- as.list("42.36354942,-71.06396087")


Sapply Function:

library(RCurl)
library(tidyjson)
library(magrittr)
library(dplyr)

PullFromPlaces <- function(x) {

url = paste0("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=",x,"&radius=",radius_meters,"&types=",type,"&key=",key)
payload_json <- getURL(url)

next_page_token <- payload_json %>%
as.tbl_json %>%
enter_object("next_page_token")
next_page_token <- as.character(attr(next_page_token,"JSON"))

if (length(next_page_token) != 0) {

url = paste0("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=",x,"&radius=",radius_meters,"&types=",type,"&pagetoken=",next_page_token,"&key=",key)
payload_json <- getURL(url)

}
}


Sapply Execution:

Output <- sapply(LatLongList, PullFromPlaces)

Answer Source

I figured it out. Below is the function to throw into sapply or lapply. The radius, type and key parameters are pre-defined.

library(jsonlite)
library(RCurl)
library(tidyjson)
library(magrittr)
library(stringr)
library(plyr)
library(dplyr)

PullFromPlaces <- function(x) {

      url = paste0("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=",x,"&radius=",radius_meters,"&types=",type,"&key=",key)
      payload_json <- getURL(url)

      next_page_token <- payload_json %>%        
        as.tbl_json %>% 
        enter_object("next_page_token") 
      next_page_token <- as.character(attr(next_page_token,"JSON"))

      if (length(next_page_token) == 0) {

        payload_json <- data.frame(payload_json,stringsAsFactors = FALSE)

      }   else {
        Sys.sleep(2)
        url2 = paste0("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=",x,"&radius=",radius_meters,"&types=",type,"&pagetoken=",next_page_token,"&key=",key)
        payload_json2 <- getURL(url2)

        next_page_token <- payload_json2 %>%        
          as.tbl_json %>% 
          enter_object("next_page_token") 
        next_page_token <- as.character(attr(next_page_token,"JSON"))

        if (length(next_page_token) == 0) {

          payload_json <- rbind_pages(list(data.frame(payload_json,stringsAsFactors = FALSE),data.frame(payload_json2,stringsAsFactors = FALSE)))

        }   else {
          Sys.sleep(2)
          url3 = paste0("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=",x,"&radius=",radius_meters,"&types=",type,"&pagetoken=",next_page_token,"&key=",key)
          payload_json3 <- getURL(url3)

          payload_json <- rbind_pages(list(data.frame(payload_json,stringsAsFactors = FALSE),data.frame(payload_json2,stringsAsFactors = FALSE),data.frame(payload_json3,stringsAsFactors = FALSE)))

        } 

      }
    }    
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download