R/create_url.R

#' Create URL.
#' 
#' This function uses httr and josonlite to import data from the Google API.
#' This function also imports additional map dat from elsewhere.
#' 
#' @param City string 
#' @return lat_long list The coordinates of a city in Sweden.
#' @author Martin Smelik, Raymond Sseguya, Vinay
#' @import plotly 
#' @import dplyr
#' @export

         

create_url <- function(City)
{
  url_google <- paste("https://maps.googleapis.com/maps/api/geocode/json?components=locality:",City,"|country:SE&key=AIzaSyChDWcgnDX4cd4CXGMEPm8flwmrP8CIals",sep="")
  
  google_data <- httr::GET(url = url_google)
  
  names(google_data)
  google_data$status_code
  google_data$headers
  google_data$headers$`content-type`
  
  stopifnot(google_data$status == 200)
 # {
#    stop
#  }
  # get the content of a response
  google_data_text_content <- 
    httr::content(google_data, "text", encoding = "UTF-8")
  google_data_text_content
  
  
  # Parse with httr
  #  google_parsed_content <- content(google_data, "parsed")
  #  names(google_parsed_content)
  #  str(google_parsed_content$results)
  #  str(google_parsed_content$status)
  #  google_parsed_content$results
  
  
  # Parse with jsonlite
  google_json_content <- jsonlite::fromJSON(google_data_text_content)
  #%>% jsonlite::fromJSON
  google_json_content
  stopifnot(google_json_content$status == "OK")
  
  
  # looking at the results dataframe
  about_city <- google_json_content$results
  names(about_city)
  
  # getting geometry of linkoeping town
  about_city$geometry
  names(about_city$geometry)
  about_city$geometry$location
  lat_long <- about_city$geometry$location
  return(as.list(lat_long))
  
}
Vinay-b-a/Lab05 documentation built on May 14, 2019, 7:16 p.m.