尝试这个
replacement_function <- function (lat, lon, benchmark, vintage)
{
if (missing(benchmark)) {
benchmark <- "Public_AR_Census2020"
}
else {
benchmark <- benchmark
}
if (missing(vintage)) {
vintage <- "Census2020_Census2020"
}
else {
vintage <- vintage
}
call_start <- "https://geocoding.geo.census.gov/geocoder/geographies/coordinates?"
url <- paste0("x=", lon, "&y=", lat)
benchmark0 <- paste0("&benchmark=", benchmark)
vintage0 <- paste0("&vintage=", vintage, "&format=json")
url_full <- paste0(call_start, url, benchmark0, vintage0)
r <- httr::GET(url_full)
httr::stop_for_status(r)
response <- httr::content(r)
return(response$result$geographies$`Census Blocks`[[1]]$GEOID)
if (length(response$result$geographies$`2020 Census Blocks`[[1]]$GEOID) ==
0) {
message(paste0("Lat/lon (", lat, ", ", lon, ") returned no geocodes. An NA was returned."))
return(NA_character_)
}
else {
if (length(response$result$geographies$`2020 Census Blocks`[[1]]$GEOID) >
1) {
message(paste0("Lat/lon (", lat, ", ", lon, ") returned more than geocode. The first match was returned."))
}
return(response$result$geographies$`2020 Census Blocks`[[1]]$GEOID)
}
}
您可以像调用 call_geolocator_latlon() 函数一样调用此函数
replacement_function(40.61847, -74.02123)