简体   繁体   English

R中的mapdist和googleway

[英]mapdist and googleway in R

I have been trying map distance between 2 postcodes. 我一直在尝试2个邮政编码之间的地图距离。 I have about ~30k records. 我有大约3万条记录。 I understand Google only allows 2500 queries/day and therefore I now have an API from them. 我了解Google每天只允许2500个查询,因此现在有了它们的API。 For some reason - I have been struggling to insert the API key into the code. 由于某些原因-我一直在努力将API密钥插入代码中。 I also came across another package called googleway - which does the same thing however I liked the format of mapdist. 我还遇到了另一个名为googleway的软件包-可以完成相同的操作,但是我喜欢mapdist的格式。 Is there a way: 有没有办法:

a) to use the API into this code OR a)在此代码中使用API​​或
b) Use google way package to populate similar results as from mapdist b)使用Google Way软件包填充与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

As far as I can tell you can't specify the API key with ggmap::mapdist 据我所知,您不能使用ggmap::mapdist指定API密钥

As for googleway (I wrote the package), the way to use it is 至于googleway (我写过程序包),使用方法是

key <- "your_api_key"
google_distance(origins = list("MCG, Melbourne, Australia"),
                destinations = list("Sydney Opera House, Australia"),
                key = key)

# $destination_addresses
# [1] "Sydney NSW, Australia"
# 
# $origin_addresses
# [1] "Jolimont Station, Wellington Cres, East Melbourne VIC 3002, Australia"
# 
# $rows
# elements
# 1 869 km, 869270, 8 hours 51 mins, 31847, 8 hours 45 mins, 31485, OK
# 
# $status
# [1] "OK"

or if you have a data.frame of from/to addresses: 或者您的data.frame来自/到地址:

df <- data.frame(from = c("MCG, Melbourne, Australia", "Sydney Opera House, Australia"),
                 to = c("Sydney Opera House, Australia", "Canberra, Australia"))


res <- apply(df, 1, function(x){

    google_distance(origins = list(x["from"]),
                    destinations = list(x["to"]),
                    key = key)

})
res
# [[1]]
# [[1]]$destination_addresses
# [1] "Sydney NSW, Australia"
# 
# [[1]]$origin_addresses
# [1] "Jolimont Station, Wellington Cres, East Melbourne VIC 3002, Australia"
# 
# [[1]]$rows
# elements
# 1 869 km, 869270, 8 hours 51 mins, 31847, 8 hours 44 mins, 31451, OK
# 
# [[1]]$status
# [1] "OK"
# 
# 
# [[2]]
# [[2]]$destination_addresses
# [1] "Canberra ACT 2601, Australia"
# 
# [[2]]$origin_addresses
# [1] "Sydney NSW, Australia"
# 
# [[2]]$rows
# elements
# 1 286 km, 286143, 3 hours 1 min, 10859, 3 hours 6 mins, 11152, OK
# 
# [[2]]$status
# [1] "OK"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM