Created
August 11, 2015 11:40
-
-
Save christopherlovell/dc6db08f9152270d48b0 to your computer and use it in GitHub Desktop.
Google places API intro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Google places API script | |
library(rjson) | |
postcode = "EC2R8AH" | |
API_request <- "https://maps.googleapis.com/maps/api/geocode/json?address=" | |
API_request <- paste(API_request,postcode,sep="") | |
address <- rjson::fromJSON(file=API_request) | |
if(address$status != "OK"){ | |
stop("Error in API look up") | |
} | |
latitude <- address$results[[1]]$geometry$location$lat | |
longitude <- address$results[[1]]$geometry$location$lng | |
# insert your own API key here | |
key = "#############################" | |
API_request <- "https://maps.googleapis.com/maps/api/place/nearbysearch/json?radius=2000&types=atm" | |
if(!is.null(key)){ | |
API_request <- paste(API_request,paste("&key=",key,sep=""),sep="") | |
} | |
API_request <- paste(API_request,"&location=", paste(latitude,longitude,sep=","),sep="") | |
bank_address <- rjson::fromJSON(file = API_request, unexpected.escape = "keep") | |
if(bank_address$status != "OK"){ | |
stop("Error in API look up") | |
} | |
library(data.table) | |
bank.details <- rbindlist(lapply(bank_address$results, function(x) data.frame(as.numeric(x$geometry$location$lat[[1]]), | |
as.numeric(x$geometry$location$lng[[1]]), | |
as.character(x$name[[1]]), | |
as.character(x$vicinity[[1]])))) | |
nrow(bank.details) | |
setnames(bank.details, c("Latitude","Longitude","Name","Address")) | |
library(ggmap) | |
map <- get_map(location = c(longitude,latitude), zoom = 14, maptype = "roadmap", scale = 2) | |
ggmap(map) + | |
geom_point(data = bank.details, aes(x = Longitude, y = Latitude, colour = Name), | |
size = 8, alpha = 0.3, shape = 19) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment