knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
This package has a geoCode.R file which has two functions. Named latlon and reverselatlon.
This function calls the google geocode API https://developers.google.com/maps/documentation/geocoding/start
To run this you need get your own API key from google, which can be taken form the link above. The functions needs two arguments address and apikey. and returns lat and long of the address in a Vector.
latlon <- function(address, apiKey){ param <- gsub("[\\@\\#\\%\\&\\!\\(\\)\\_\\*\\+\\;\\^]", "", address) params <- trimws(gsub("\\s+","+", param)) base <- "https://maps.googleapis.com/maps/api/geocode/json?address=" urlAPI <- paste0(base,params,"&key=",apiKey) callAPI <- GET(urlAPI) content(callAPI) resultJSON <- fromJSON(txt = callAPI$url) X <- resultJSON$results$geometry$location$lng Y <- resultJSON$results$geometry$location$lat return(c(X,Y)) }
This function also call google geocode but reverse geocode this function needs three arguments. The lat, long, and apikey. It returns the address of the given lat long.
reverselatlon<- function(lat, lon, apiKey){ params <- paste0(lat,",",lon) base <- "https://maps.googleapis.com/maps/api/geocode/json?latlng=" urlAPI <- paste0(base,params,"&key=",apiKey) callAPI <- GET(urlAPI) content(callAPI) resultJSON <- fromJSON(txt = callAPI$url) address <- resultJSON$results$formatted_address return(address) }
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.