1

我连续收到所有“Google Map API Requests”,但是当我尝试循环调用和解析它时。我收到一个错误。如果我不使用循环并手动执行它,它就可以工作。

 a <- c("1780 N Washington Ave Scranton PA 18509", "1858 Hunt Ave Bronx NY 10462", "140 N Warren St Trenton NJ 08608-1308")

#API Key need to be added to run:
 w <- c("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=19+East+34th+Street+New York+NY+10016&destinations=1780+N+Washington+Ave+Scranton+PA+18509&mode=transit&language=fr-FR&key=API_KEY_HERE",
   "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=19+East+34th+Street+New York+NY+10016&destinations=1858+Hunt+Ave+Bronx+NY+10462&mode=transit&language=fr-FR&key=API_KEY_HERE",
   "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=19+East+34th+Street+New York+NY+10016&destinations=140+N+Warren+St+Trenton+NJ+08608-1308&mode=transit&language=fr-FR&key=API_KEY_HERE")

df <- data.frame(a,w)

for (i in cpghq) {
  url <- df$w
  testdf <- jsonlite::fromJSON(url, simplifyDataFrame = TRUE)
  list <- unlist(testdf$rows)
  transit_time <- as.data.frame(t(as.data.frame(list)))
  cpghq$transit_time <- transit_time

我得到的错误是:

Error: lexical error: invalid char in json text.
                                   https://maps.googleapis.com/map
                 (right here) ------^ 
4

2 回答 2

1

我的 API 调用是错误的,因为“纽约”有空间。我使用 gsub("[[:space:]]", "+", a) 进行了修复,但 utils::URLencode() 也会起作用。

构建 API 调用

 a <- c("1780 N Washington Ave Scranton PA 18509", "1858 Hunt Ave Bronx NY 10462", "140 N Warren St Trenton NJ 08608-1308")

  fix_address <- gsub("[[:space:]]", "+", a)

  key <- "YOUR_GOOGLE_API_KEY_HERE"
  travel_mode <- "transit"

  root <- "https://maps.googleapis.com/maps/api/distancematrix/json
  units=imperial&origins="
  api_call <- paste0(root,"350+5th+Ave+New+York+NY+10118", 
                    "&destinations=",
                    fix_address,
                    "&mode=", 
                    travel_mode,
                    "&language=en-EN",
                    "&key=", key)
 

我的循环问题非常简单。我没有使用 lapply()

现在使用 RSJSONIO::fromJSON 将调用发送到 Google Map API

 require("RJSONIO")
 if(verbose) cat(address,"\n")
 # Get json returns from Google
doc   <- lapply(api_call, RCurl::getURL) 
于 2016-06-06T14:25:36.613 回答
1

正如我在其他回答中指出的那样,您也可以使用我googleway为您完成工作。

library(googleway)

key <- "your_api_key"

a <- c("1780 N Washington Ave Scranton PA 18509", 
       "1858 Hunt Ave Bronx NY 10462", 
       "140 N Warren St Trenton NJ 08608-1308")

google_distance(origins = "350 5th Ave New York NY 10188",
                destinations = as.list(a),
                mode = "transit",
                key = key, 
                simplify = T)


# $destination_addresses
# [1] "1780 N Washington Ave, Scranton, PA 18509, USA" "1858 Hunt Ave, Bronx, NY 10462, USA"           
# [3] "140 N Warren St, Trenton, NJ 08608, USA"       
# 
# $origin_addresses
# [1] "Empire State Building, 350 5th Ave, New York, NY 10118, USA"
# 
# $rows
# elements
# 1 ZERO_RESULTS, OK, OK, NA, 19.0 km, 95.8 km, NA, 18954, 95773, NA, 54 mins, 1 hour 44 mins, NA, 3242, 6260
# 
# $status
# [1] "OK"
于 2016-06-27T23:35:25.833 回答