2017-10-12 75 views
1

我从基于谷歌地图API搜索附近请求的银行提取数据。在某些情况下,它拉扯多于一家银行(因为一些银行可能靠近)。我如何去解压返回的每个单独的JSON对象(因为我需要获取地点ID),以便我可以执行第二次api拉(基于地点ID)以搜索每个银行的更多详细信息?以下是我的代码:从JSON中提取行Google API请求中的R

require(jsonlite) 
require(utils) 


plcUrl <- "https://maps.googleapis.com/maps/api/place/nearbysearch/json?" 
key <- "myKEY" 
location <- paste0("41.0272, -81.51345") 
address <- "XXXXXXXXXXXXXXXXX" 
type <- "bank" 
radius <- "500" 
name = "XXXXX" 
strurl <- as.character(paste(plcUrl , 
          "&location=",location, 
          "&address=",address, 
          #"&name=",name, 
          "&radius=",radius, 
          "&type=",type, 
          "&key=",key, 
          sep="")) 
setInternet2(TRUE) 
rd <- fromJSON(URLencode(strurl)) 
rd$results$place_id 

回答

3

从googleway v2.4开始,我添加了访问Google API查询特定元素的方法。

library(googleway) 

key <- "your_api_key" 

## search places 
res <- google_places(location = c(41.0272, -81.51345), 
        key = key, 
        place_type = "bank", 
        radius = 500) 


## get the place_id values using the `place` method to extract the ids 
place(res) 
# [1] "ChIJDe7R2HQqMYgRvqoszlV6YTA" "ChIJDQwLUXMqMYgR-3Nb2KFhZZ0" 

## query the details 
details <- google_place_details(place_id = place(res)[1], key = key) 

details$result$opening_hours 
# $open_now 
# [1] FALSE 
# 
# $periods 
# close.day close.time open.day open.time 
# 1   1  1600  1  0900 
# 2   2  1600  2  0900 
# 3   3  1600  3  0900 
# 4   4  1600  4  0900 
# 5   5  1800  5  0900 
# 6   6  1300  6  0900 
# 
# $weekday_text 
# [1] "Monday: 9:00 AM – 4:00 PM" "Tuesday: 9:00 AM – 4:00 PM" "Wednesday: 9:00 AM – 4:00 PM" "Thursday: 9:00 AM – 4:00 PM" 
# [5] "Friday: 9:00 AM – 6:00 PM" "Saturday: 9:00 AM – 1:00 PM" "Sunday: Closed" 
+0

谷歌尚未访问开放时间吗?由于这个事实,我已经远离你的包裹,因为那正是我所需要的。谢谢! – CooperBuckeye05

+0

@ CooperBuckeye05 - 当然可以,看我的编辑 – SymbolixAU

+0

真棒,非常感谢你! – CooperBuckeye05