Skip to content

Instantly share code, notes, and snippets.

@kimjoaoun
Last active January 8, 2020 19:34
Show Gist options
  • Save kimjoaoun/a576102850b806035eaf72203fa80ab3 to your computer and use it in GitHub Desktop.
Save kimjoaoun/a576102850b806035eaf72203fa80ab3 to your computer and use it in GitHub Desktop.
HERE Maps API - Maina
set_key <- function(api_key){
Sys.setenv(
"HERE_API_KEY" = api_key
)
}
geocode_manual <- function(address) {
ansr <- data.frame(NivelMatch = NA, Address = NA, postal_code = NA, distrito_bairro= NA, cidade = NA, estado= NA, pais= NA, Latitude= NA, Longitude = NA)
request <- GET(
url = "https://geocoder.ls.hereapi.com/6.2/geocode.json?",
query = list(
apiKey = Sys.getenv("HERE_API_KEY"),
searchtext = address
)
)
response <- content(request, as = "text", encoding = "UTF-8")
df <- fromJSON(response, flatten = TRUE)
if (is.null(df$Response$View$Result)) {
k = NA
} else {
k <- data.frame(df$Response$View$Result)
k <- k[1, ] %>% select(
MatchLevel,
Location.Address.Label,
Location.Address.PostalCode,
Location.Address.District,
Location.Address.City,
Location.Address.State,
Location.Address.Country,
Location.NavigationPosition
) %>% rename(
NivelMatch = MatchLevel,
Address = Location.Address.Label,
postal_code = Location.Address.PostalCode,
distrito_bairro = Location.Address.District,
cidade = Location.Address.City,
estado = Location.Address.State,
pais = Location.Address.Country
)
k = cbind(k, k$Location.NavigationPosition[[1]])
k = k %>% select(-Location.NavigationPosition)
}
ansr[1, ] = k
ansr
}
result = map_dfr(.x = enderecos_aqui, .f= geocode_manual)
result = tibble::rowid_to_column(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment