Multiple Google Places API calls within Sapply function

时间秒杀一切 提交于 2019-12-24 08:12:48

问题


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)

回答1:


I have an example of such a query in the vignette of my googleway package

library(googleway)


api_key <- 'your_api_key'

myLocation <- c(42.36354942, -71.06396078)

myPlaces <- google_places(location = myLocation, 
                        radius = 500,
                        key = api_key)

nextPlaces <- google_places(location = myLocation,
                            radius = 500,
                            page_token = myPlaces$next_page_token, 
                            key = api_key)


myPlaces$results$name
# [1] "Boston"                                              "Kimpton Onyx Hotel"                                 
# [3] "Holiday Inn Express Hotel & Suites Boston Garden"    "Wyndham Boston Beacon Hill"                         
# [5] "The Boxer Boston Hotel"                              "Whole Foods Market"                                 
# [7] "The Liberty, a Luxury Collection Hotel, Boston"      "Massachusetts General Hospital"                     
# [9] "TD Garden"                                           "Sugarman, Rogers, Barshak & Cohen, P.C."            
# [11] "Dr. Richard J. Deasla, MD"                           "Massachussetts General Hospital"                    
# [13] "Massachusetts General Hospital: Temel Jennifer S MD" "Vrahas Mark Steven MD"                              
# [15] "Harry E. Rubash, MD"                                 "Dr. Ziv Williams, MD"                               
# [17] "CCRM Boston"                                         "Domino's Pizza"                                     
# [19] "Massachusetts General Hospital: Yeh Sunu Susan MD"   "North End"

nextPlaces$results$name
# [1] "Massachusetts General Hospital: Ryan Colleen MD"         "Warshaw Andrew L MD"                                    
# [3] "Massachusetts General Hospital: Althausen Anne M MD"     "Massachusetts General Hospital: Shipley William MD"     
# [5] "Massachusetts General Hospital: Feldman Adam S MD"       "Massachusetts General Hospital: Packard Swift Alison MD"
# [7] "Dr. Nahel Elias, MD"                                     "Dr. Steven L. Mcafee, MD"                               
# [9] "Dr. Charles A. Welch, MD"                                "Massachusetts General Hospital: Kilbride Ronan D MD"    
# [11] "Massachusetts General Hospital: Garasic Joseph Mich MD"  "Massachusetts General Hospital: Akins Cary W MD"        
# [13] "Dr. Edwin C. Huang, MD"                                  "Massachusetts General Hospital: Davis Benjamin T MD"    
# [15] "Massachusetts General Hospital: Levins Paul C MD"        "Massachusetts General Hospital: Passeri Jonathan MD"    
# [17] "Massachusetts General Hospital: Weil Michelle MD"        "Massachusetts General Hospital: Steele David John MD"   
# [19] "Massachusetts General Hospital: Chae Claudia U MD"       "Massachusetts General Hospital: Connolly Thomas Jose MD"



回答2:


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)))

        } 

      }
    }    


来源:https://stackoverflow.com/questions/45242400/multiple-google-places-api-calls-within-sapply-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!