I have been trying map distance between 2 postcodes. I have about ~30k records. I understand Google only allows 2500 queries/day and therefore I now have an API from them. For some reason - I have been struggling to insert the API key into the code. I also came across another package called googleway - which does the same thing however I liked the format of mapdist. Is there a way:
a) to use the API into this code OR
b) Use google way package to populate similar results as from mapdist
Appreciate your support in advance.
library(ggmap)
library(plyr)
library(googleway)
key <- "XXX"
file_loc <- "C:/Users/Owner/Desktop/distance.csv"
x <- read.csv(file_loc, header = TRUE)
from <- x[1]
to <- x[2]
DF <- cbind(from, to); DF <- as.data.frame(DF) # create data frame
DF$from <- as.character(DF$from) # mapdist demands input to be character type
DF$to <- as.character(DF$to) # mapdist demands input to be character type
remove (from, to) #remove input to avoid confusion
DF$row.number <- 1:nrow(DF) #create an index number for each row
for (i in DF$row.number){
orig <- DF[i,c('from')]
dest <- DF[i,c('to')]
a <- mapdist(from = orig, to = dest, mode = "driving",output = c("simple", "all"), override_limit = FALSE)
a$row.number <- i
DF$minutes[match(a$row.number, DF$row.number)] <- a$minutes
DF$hours[match(a$row.number, DF$row.number)] <- a$hours
DF$km[match(a$row.number, DF$row.number)] <- a$km
DF$miles[match(a$row.number, DF$row.number)] <- a$miles
}
write.csv(DF, "newdata.csv") #Save dataset