Skip to content

Instantly share code, notes, and snippets.

@PhillRob
Last active September 17, 2015 08:21
Show Gist options
  • Save PhillRob/14abef404c678934cfe4 to your computer and use it in GitHub Desktop.
Save PhillRob/14abef404c678934cfe4 to your computer and use it in GitHub Desktop.
long/lat data from location description
getGeoCode <- function(gcStr, countryID) {
#library("RJSONIO") #Load Library
print(gcStr)
apik<-"AIzaSyCAaXX1jrJoD19uHBUnL97xpegOUxsDeSw"
apik1<-"AIzaSyA1rQIGrUfizL6E4mE22RhUh_X5saYPMU0"
gcStr <- gsub(' ','%20',gcStr) #Encode URL Parameters
#Open Connection
data.json <- fromJSON(paste(getURL(paste('https://maps.google.com/maps/api/geocode/json?sensor=false&address=',gcStr, '&components=country:',countryID,sep=""))))
#'&key=',apik1
#Flatten the received JSON
if (data.json$status=="OK"){
if((is.na(data.json$status)) |
(is.na(data.json$results[[1]]$geometry$location_type)))
{return("NA")} else{
if ((data.json$status=="OK")&&
(data.json$results[[1]]$geometry$location_type!="APPROXIMATE"))
{
lng<-data.json$results[[1]]$geometry$location[2]
lat<-data.json$results[[1]]$geometry$location[1]
typ<-data.json$results[[1]]$geometry$location_type
gcodes <- c(lat, lng, typ)
names(gcodes) <- c("Lat", "Lng","Type")
return (gcodes)
}else
{return("NA")}}}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment