[英]Why can I bypass my firewall to access Google Distance Matrix API with ggmap, but not googleway or gmapsdistance in R?
[英]Building dataframes from Google Maps API using googleway in R
我写了一个 function,它将从谷歌地图 API 中提取地理空间数据,并使用 googleway function 'google_places' 将 1) 名称 2) 坐标存储在 dataframe 中。
Google_places 使用“下一页标记”进行一次完整搜索的总共 3 次 API 调用。
当拉出 function 时,这段代码完美运行并返回 60 行 dataframe。
但是,当我使用适当的 arguments 运行此 function 时,它返回只有 40 个结果的 dataframe。
我在我的代码中明确表示要运行所有三个必要的调用,而不仅仅是两个。
我不确定为什么此代码在 function 之外有效,但在 function 内部无效。
有谁知道这里发生了什么?
同样,这是使用 googleway google_places function。https://rdrr.io/cran/googleway/man/google_places.html
谢谢:这是代码:
一世。 首先设置搜索使用的条件
search_string <- "Urgent care center"
key <- key #my API key
radius <- 50000
location <- L1
#L1 is a numeric vector of coordinates [1] 39 -105
search <- google_places(search_string, key, location, radius)
二. Function 通过从一个搜索调用创建 dataframe 来初始化搜索(总共有三个搜索调用)。
thin_df <- function(search){
a <- search$results$name
b <- search$results$geometry$location$lat
c <- search$results$geometry$location$lng
thin_df <- data.frame(a, b, c)
return(thin_df)}
三. 在这个 function 中,“central df”结合了三个“thin df”调用结果,使用先前定义的 arguments 和 thin_df function 创建对第一个坐标对的完整搜索。
full_search <- function(search_string,
key, location, radius){
call_1 <- google_places(search_string,
key,
location,
radius)
thin1 <- thin_df(call_1)
call_2 <- google_places(search_string = search_string,
page_token = call_1$next_page_token,
key = key,
location = location,
radius = radius)
thin2 <- thin_df(call_2)
full_df <- rbind(thin1, thin2)
call_3 <- google_places(search_string,
page_token = call_2$next_page_token,
key,
location,
radius)
thin3 <- thin_df(call_3)
central_df <- rbind(full_df, thin3)
return(central_df)
}
central_df <- full_search(("Urgent care center", key, L1, 50000)
我怀疑您的问题是您违反了每秒可以发出的请求数。
在我这里的例子中
while
循环来控制多个调用Sys.sleep(2)
调用来暂停 function 2 秒access_results
来获取结果的特定组件(这只是一个方便的功能)res$status
的状态,只有在"OK"
的情况下才尝试获取内容这只是我放在一起的一些东西,目的是给你一些关于如何让它工作和处理一些常见错误的想法。 随意更改它以适合您的编程风格/要求。
search_string <- "Urgent care center"
key <- secret::get_secret("GOOGLE")
radius <- 50000
location <- c(39, -105)
format_res <- function(res) {
setNames(
cbind(
googleway::access_result(res, "coordinates")
, googleway::access_result(res, "place_name")
)
, c("lat", "long", "name")
)
}
do_search <- function(search_string, key, location, radius, page_token = NULL) {
google_places(
search_string = search_string
, location = location
, key = key
, radius = radius
, page_token = page_token
)
}
full_search <- function(search_string, key, location, radius) {
counter <- 0
page_token <- NULL ## can start on NULL because it means we're doing the first query
is_another_page <- TRUE
while( is_another_page ) {
res <- do_search(search_string, key, location, radius, page_token)
if( res$status == "OK" ) { ## check a valid result was returned
if( counter == 0 ) {
df <- format_res( res )
} else {
df <- rbind(df, format_res( res ) )
}
counter <- counter + 1
}
page_token <- res[["next_page_token"]]
is_another_page <- !is.null( page_token )
Sys.sleep(2) ## Sleep the function before the next call because there's a time limit
}
return(df)
}
df <- full_search(search_string, key, location, radius)
str( df )
# 'data.frame': 60 obs. of 3 variables:
# $ lat : num 38.9 38.8 38.9 38.9 38.9 ...
# $ long: num -105 -105 -105 -105 -105 ...
# $ name: chr "UCHealth Urgent Care - Garden of the Gods" "UCHealth Urgent Care - Circle Square" "Penrose Community Urgent Care" "Emergicare Austin Bluffs" ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.