R/geocodePlace.R

Defines functions geocodePlace

Documented in geocodePlace

geocodePlace <- function(name, rough.crd, radius = 50000, key, result = 1,
                         pause.time = 0.02) {
  if (grepl("&", name)) {
    name <- gsub("&", "", name)
  }
  name <- iconv(name, to = "ASCII//TRANSLIT")
  a <- "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="
  b <- "&radius="
  c <- "&name="
  d <- "&key="
  url <- paste0(a, rough.crd[2], ",", rough.crd[1], b, radius, c, name, d, key)
  doc <- URLencode(url)
  x <- RJSONIO::fromJSON(doc, simplify = FALSE)
  out <- list()
  if(x$status == "OK") {
    r <- result
    out$crd <- c(x$results[[r]]$geometry$location$lng,
                 x$results[[r]]$geometry$location$lat)
    names(out$crd) <- c("lon", "lat")
    out$name <- x$results[[r]]$name
    out$types <- x$results[[r]]$types
    out$vicinity <- x$results[[r]]$vicinity
    return(out)
  } else {
    return(list(crd = c(NA, NA), name = NA, types = NA, vicinity = NA))
    warning("Request Status: ", x$status)
  }
  Sys.sleep(pause.time)
}
walshc/GoogleMapsAPI documentation built on May 3, 2019, 11:50 p.m.