Here is a function based on Harvey's suggestion. It will look for the address and give the coordinates of the first result. Have a look at the structure of x
in the function to see other information you can get.
geocodeAdddress <- function(address) {
require(RJSONIO)
url <- "http://maps.google.com/maps/api/geocode/json?address="
url <- URLencode(paste(url, address, "&sensor=false", sep = ""))
x <- fromJSON(url, simplify = FALSE)
if (x$status == "OK") {
out <- c(x$results[[1]]$geometry$location$lng,
x$results[[1]]$geometry$location$lat)
} else {
out <- NA
}
Sys.sleep(0.2) # API only allows 5 requests per second
out
}
For example:
R> geocodeAdddress("Time Square, New York City")
[1] -73.98722 40.7575
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…