如何从谷歌地图API获取开车时间?

时间:2022-05-25 15:26:10

I use the following function to estimate the time (in hours) to drive a certain distance, assuming an average speed of 65 km/h:

我使用下面的函数估计行驶一定距离的时间(以小时为单位),假设平均速度为65公里/小时:

distHoras <- function(origin, destination){
  xml.url <- paste0('http://maps.googleapis.com/maps/api/distancematrix/xml?origins=',
                    origin, '&destinations=', destination, '&mode=driving&sensor=false')
  xmlfile <- xmlParse(getURL(xml.url))
  dist <- xmlValue(xmlChildren(xpathApply(xmlfile,"//distance")[[1]])$value)
  distance <- as.numeric(sub(" km", "", dist))
  time <- (distance / 1000) / 65
  return(time)
}

How can I tweak this function in order to have it yield time directly, so I don't need to make this 65 km/h assumption and thus get a better estimate? After reading the documentation, I tried switching 'distance' with 'duration', but it didn't work. I'm probably missing something simple, but I'm quite new to working with APIs and am overwhelmed by all that text. Appreciate any help!

我如何调整这个函数,以便让它直接产生时间,这样我就不需要做这个65公里/小时的假设,从而得到更好的估计?看完文档后,我试着用“持续时间”来切换“距离”,但没有用。我可能漏掉了一些简单的东西,但是我对使用api很陌生,并且被所有的文本所淹没。感谢任何帮助!

2 个解决方案

#1


4  

Are you looking for this :

你在找这个吗:

library(ggmap)
from <- 'Paris'
to <- 'London'
mapdist(from,to,mode='driving')
 from     to      m      km    miles seconds  minutes    hours
1 Paris London 454416 454.416 282.3741   18283 304.7167 5.078611

mapdist Compute map distances using Google Maps.

mapdist使用谷歌映射计算地图距离。

To answer your question, I think it is easier (even recommended) to use json version of google API than XML one.

为了回答您的问题,我认为使用json版本的谷歌API比使用XML更容易(甚至建议)。

Here a fast version using RJSONIO. Even I recommend you to use the function above. No need to do any conversion since the result is already in hours.

这里有一个使用RJSONIO的快速版本。甚至我也建议您使用上面的功能。不需要做任何转换,因为结果已经在数小时内。

library(RJSONIO)
distHoras <- function(origin, destinations){

origin <- gsub(",", "", origin)
origin <- gsub(" ", "+", origin)
origin <- paste("origins=", origin, sep = "")

destinations <- gsub(",", "", destinations)
destinations <- gsub(" ", "+", destinations)
destinations <- paste("destinations=", paste(destinations, 
                                             collapse = "|"), sep = "")


mode4url <- paste("mode=", 'driving', sep = "")
lang4url <- paste("language=", 'en-EN', sep = "")
sensor4url <- paste("sensor=", tolower(as.character(FALSE)), 
                   sep = "")
posturl <- paste(origin, destinations, mode4url, sensor4url, 
                 sep = "&")
url_string <- paste("http://maps.googleapis.com/maps/api/distancematrix/json?", 
                    posturl, sep = "")
url_string <- URLencode(url_string)
connect <- url(url_string)
tree <- fromJSON(paste(readLines(connect), collapse = ""))
close(connect)
rapply(tree$rows,I)
}

Now you test it :

现在你测试它:

distHoras('Paris','London')
 elements.distance.text elements.distance.value  elements.duration.text 
               "454 km"                "454416"        "5 hours 5 mins" 
elements.duration.value         elements.status 
                "18283"                    "OK" 

#2


0  

I'm going to throw my own pacakge into the mix that also queries Google's API for you

我将把我自己的pacakge加入到为您查询谷歌的API的混合中

(You need a valid Google API key to use it)

(您需要一个有效的谷歌API密钥来使用它)

library(googleway)

api_key <- "your_api_key_here"

google_distance(origins = "Paris", 
                                destinations = "London",
                                key = api_key)

# $destination_addresses
# [1] "London, UK"
# 
# $origin_addresses
# [1] "Paris, France"
# 
# $rows
# elements
# 1 456 km, 456230, 5 hours 31 mins, 19858, 6 hours 12 mins, 22311, OK
# 
# $status
# [1] "OK"

#1


4  

Are you looking for this :

你在找这个吗:

library(ggmap)
from <- 'Paris'
to <- 'London'
mapdist(from,to,mode='driving')
 from     to      m      km    miles seconds  minutes    hours
1 Paris London 454416 454.416 282.3741   18283 304.7167 5.078611

mapdist Compute map distances using Google Maps.

mapdist使用谷歌映射计算地图距离。

To answer your question, I think it is easier (even recommended) to use json version of google API than XML one.

为了回答您的问题,我认为使用json版本的谷歌API比使用XML更容易(甚至建议)。

Here a fast version using RJSONIO. Even I recommend you to use the function above. No need to do any conversion since the result is already in hours.

这里有一个使用RJSONIO的快速版本。甚至我也建议您使用上面的功能。不需要做任何转换,因为结果已经在数小时内。

library(RJSONIO)
distHoras <- function(origin, destinations){

origin <- gsub(",", "", origin)
origin <- gsub(" ", "+", origin)
origin <- paste("origins=", origin, sep = "")

destinations <- gsub(",", "", destinations)
destinations <- gsub(" ", "+", destinations)
destinations <- paste("destinations=", paste(destinations, 
                                             collapse = "|"), sep = "")


mode4url <- paste("mode=", 'driving', sep = "")
lang4url <- paste("language=", 'en-EN', sep = "")
sensor4url <- paste("sensor=", tolower(as.character(FALSE)), 
                   sep = "")
posturl <- paste(origin, destinations, mode4url, sensor4url, 
                 sep = "&")
url_string <- paste("http://maps.googleapis.com/maps/api/distancematrix/json?", 
                    posturl, sep = "")
url_string <- URLencode(url_string)
connect <- url(url_string)
tree <- fromJSON(paste(readLines(connect), collapse = ""))
close(connect)
rapply(tree$rows,I)
}

Now you test it :

现在你测试它:

distHoras('Paris','London')
 elements.distance.text elements.distance.value  elements.duration.text 
               "454 km"                "454416"        "5 hours 5 mins" 
elements.duration.value         elements.status 
                "18283"                    "OK" 

#2


0  

I'm going to throw my own pacakge into the mix that also queries Google's API for you

我将把我自己的pacakge加入到为您查询谷歌的API的混合中

(You need a valid Google API key to use it)

(您需要一个有效的谷歌API密钥来使用它)

library(googleway)

api_key <- "your_api_key_here"

google_distance(origins = "Paris", 
                                destinations = "London",
                                key = api_key)

# $destination_addresses
# [1] "London, UK"
# 
# $origin_addresses
# [1] "Paris, France"
# 
# $rows
# elements
# 1 456 km, 456230, 5 hours 31 mins, 19858, 6 hours 12 mins, 22311, OK
# 
# $status
# [1] "OK"