2017-10-12 73 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