Sapply函数中的多个Google Places API调用 [英] Multiple Google Places API calls within Sapply function

查看:164
本文介绍了Sapply函数中的多个Google Places API调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要添加到Google Places API的位置列表.某些位置有20多个结果.我在下面提供了一个此类位置的示例.要获得超过前20个的结果,您必须对Google Places进行额外的API调用,并带有一个额外的令牌"参数,该参数是从第一个Google Places API调用中获得的.

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.

使用以下有缺陷的功能,我试图根据是否需要获取其他结果来执行其他API调用.当前函数产生NULL值.在纠正此功能方面的任何帮助将不胜感激.

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.

要提交到Sapply的列表:

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

应用功能:

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)

      }
}

应用执行:

Output <- sapply(LatLongList, PullFromPlaces)

推荐答案

我知道了.以下是放入sapply或lapply的函数.半径,类型和关键参数是预先定义的.

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

        } 

      }
    }    

这篇关于Sapply函数中的多个Google Places API调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆