I am trying to find the latitude and longitude for a list of addresses including, for example, "108 CYPRESS RD GREENE , NY 13778". However when I plug that into Google's API (using for example the textbox here) it returns another address: "300 CYPRUS LN ENDICOTT NY 13760"
Is there anything I can do to help Google's API (or RDSTK, which I have also used) to correctly identify the latitude and longitude?
I'm not sure about that website, but try using some actual R code. I found some plug-and-play code on this page: http://www.r-bloggers.com/using-google-maps-api-and-r/
library(RCurl)
library(RJSONIO)
library(plyr)
url <- function(address, return.call = "json", sensor = "false") {
root <- "http://maps.google.com/maps/api/geocode/"
u <- paste(root, return.call, "?address=", address, "&sensor=", sensor, sep = "")
return(URLencode(u))
}
geoCode <- function(address,verbose=FALSE) {
if(verbose) cat(address,"\n")
u <- url(address)
doc <- getURL(u)
x <- fromJSON(doc,simplify = FALSE)
if(x$status=="OK") {
lat <- x$results[[1]]$geometry$location$lat
lng <- x$results[[1]]$geometry$location$lng
location_type <- x$results[[1]]$geometry$location_type
formatted_address <- x$results[[1]]$formatted_address
return(c(lat, lng, location_type, formatted_address))
} else {
return(c(NA,NA,NA, NA))
}
}
address <- geoCode("108 CYPRESS RD GREENE , NY 13778")
address
## [1] "42.3106291"
## [2] "-75.7923396"
## [3] "RANGE_INTERPOLATED"
## [4] "108 Cypress Road, Greene, NY 13778, USA"
To see what it looks like:
library(ggmap)
df <- data.frame(lat = as.numeric(address[1]), lon = as.numeric(address[2]))
map <- get_map("108 CYPRESS RD GREENE , NY 13778", zoom = 18)
ggmap(map) + geom_point(aes(x=lon, y=lat), data=df,colour="red", size=6) +
ggtitle("108 CYPRESS RD GREENE , NY 13778")