谷歌地图API错误地编码地址

时间:2015-08-01 19:33:00

标签: r google-maps geocoding google-geocoder

我试图找到地址列表的纬度和经度,包括例如" 108 CYPRESS RD GREENE,NY 13778"。但是,当我将其插入Google的API时(例如使用文本框here),它会返回另一个地址: " 300 CYPRUS LN ENDICOTT NY 13760"

我可以做些什么来帮助谷歌的API(或我也使用的RDSTK)正确识别纬度和经度?

1 个答案:

答案 0 :(得分:1)

我不确定该网站,但尝试使用一些实际的R代码。我在此页面上找到了一些即插即用的代码: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"

看看它的样子:

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")

enter image description here