'Error in parse_url(url) : length(url) == 1 is not TRUE
I am trying to use google distance matrix api to receive the coordinates from a list of address (2289 addresses in total). I am trying to pull out the coordinates in latitude and longitude to each address.
# ADDRESS : a list of the N adresses to be geocoded
# LON /LAT : two matrices, size [nx1],initialised to contain only 0
Address <- as.matrix(Coordinates$Origin)
LON = matrix(0, length(Address), 1)
LAT = matrix(0, length(Address), 1)
View(LAT)
for (i in seq(1,length(Address))){
APIstring = c("https://maps.googleapis.com/maps/api/geocode/json?address=",
Address[i],",&key=AIzaSyCevHB7yTBuiDbdHd8DwE64ZvWM-NZH79s")
res = GET(APIstring)
tmp = fromJSON(content(res, as = "text"))
LAT[i] =tmp$results$geometry$location$lat
LON[i] =tmp$results$geometry$location$lng
}
Error in parse_url(url) : length(url) == 1 is not TRUE
Solution 1:[1]
Likely, your problems arises because your code trying to vectorize the GET call is not properly integrated into the loop. You might want to handle URLs one by one, and could do so using functional programming (faster than your loop, too). How about the following based on the suggestion of @Limey?
library(tidyverse)
get_lon_and_lat <- function(.address){
.api_string <- paste0(
"https://maps.googleapis.com/maps/api/geocode/json?address=",
.address,
",&key=[*insert key here*]")
.res <- GET(.api_string)
.res <- fromJSON(content(.res, as = "text"))
.out <- data.frame(
lat = .res$results$geometry$location$lat,
lon = .res$results$geometry$location$lng
)
return(.out)
}
result_lat_lon <- lapply(Address, get_lon_and_lat) %>%
bind_rows()
PS: You might want to remove your API key from your question for security reasons.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
