所以这是在 Google 上使用 reverse geocoding API 的替代方案。此代码部分基于this reference。
在df上方调用您的数据框,
reverseGeoCode <- function(latlng) {
require("XML")
require("httr")
latlng <- as.numeric(latlng)
latlngStr <- gsub(' ','%20', paste(round(latlng,2), collapse=","))
url <- "http://maps.google.com"
path <- "/maps/api/geocode/xml"
query <- list(sensor="false",latlng=latlngStr)
response <- GET(url, path=path, query=query)
if (response$status !=200) {
print(paste("HTTP Error:",response$status),quote=F)
return(c(NA,NA))
}
xml <- xmlInternalTreeParse(content(response,type="text"))
status <- xmlValue(getNodeSet(xml,"//status")[[1]])
if (status != "OK"){
print(paste("Query Failed:",status),quote=F)
return(c(NA,NA))
}
xPath <- '//result[1]/address_component[type="country"]/long_name[1]'
country <- xmlValue(getNodeSet(xml,xPath)[[1]])
xPath <- '//result[1]/address_component[type="administrative_area_level_1"]/long_name[1]'
state <- xmlValue(getNodeSet(xml,xPath)[[1]])
return(c(state=state,country=country))
}
st.cntry <- t(apply(df,1,function(x)reverseGeoCode(x[2:3])))
result <- cbind(df,st.cntry)
result
# Point_Name Longitude Latitude state country
# 1 University of Arkansas 36.06783 -94.17365 Arkansas United States
# 2 Lehigh University 40.60146 -75.36006 Pennsylvania United States
# 3 Harvard University 42.37939 -71.11590 Massachusetts United States
在 API 定义中,“administrative_area_level_1”是国家以下的最高行政区域。在美国,这些是州。在其他国家/地区,定义各不相同(例如,可能是省份)。
顺便说一句,我相当肯定你的经纬度颠倒了。