I am trying to run this code which takes a list of addresses and runs each one through Google's Geocode API (using function Addr2latlng below) to get the latitude/longitude and puts each one into a data frame using ProcessAddrList below.
The problem is Addr2latlng works fine for one address and ProcessAddrList works fine for up to 10 addresses, but from 11 addresses or more I get the error below. For 10 addresses this works fine.
To run the code below requires the packages RCurl and RJSONIO to be loaded.
Error in geoStruct$results[[1]] : subscript out of bounds
Error in geoStruct$results[[1]] : subscript out of bounds
ProcessAddrList <- function(addrList)
{
resultDF <- data.frame(atext=character(),X=numeric(),Y=numeric(),EID=numeric())
i <- 1
for (addr in addrList)
{
latlng = Addr2latlng(addr)
resultDF <-rbind(resultDF,data.frame(atext=addr,X=latlng[[2]],Y=latlng[[1]],EID=i))
i <- i+1
}
return (resultDF)
}
Addr2latlng <- function(address)
{
url <- MakeGeoURL(address)
apiResult <- getURL(url)
geoStruct <- fromJSON(apiResult, simplify = FALSE)
lat <- NA
lng <- NA
try(lat <- geoStruct$results[[1]]$geometry$location$lat)
try(lng <- geoStruct$results[[1]]$geometry$location$lng)
return(c(lat, lng))
}