Friday, 8 December 2017

Connect to Google Map API from R

The below enables geocoding from R using Google Map API.

The relevant website is:

There is a restriction on how many requests can be made per day for free usage etc, and the above website provides the necessary information and guidelines.

While it is not necessary, you can download your own key for additional benefits including tracking of the usage. Please see below link:



#if you are using a key
KEY<-as.character(read.table("//[path to your Google Key]/[name of file containing the key (e.g. '.GoogleApiKey.txt']", header=FALSE, sep = ""))

search_url <- paste("[insert address or location searched]&key=", KEY, sep = "")

#if you are NOT using a key
search_url <- "[insert address or location searched]"
con <- url(search_url, open="r")
geo_info <- fromJSON(paste(readLines(con), collapse=""))


#Flatten the received JSON
geo_info  <- unlist(geo_info )

latitude <- geo_info[""]
longitude <- geo_info["results.geometry.location.lng"]

No comments:

Post a Comment