如何从Google Maps API获取驾车时间? [英] How do I get driving time from Google Maps API?

查看:151
本文介绍了如何从Google Maps API获取驾车时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  distHoras < - 函数(原始地址,目的地){
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) )
时间< - (distance / 1000)/ 65
return(time)
}

我如何调整这个函数以使其直接产生时间,所以我不需要做这个65 km / h的假设,从而得到一个更好的估计?阅读文档后,我试着用'duration'切换'distance',但它没有工作。我可能错过了一些简单的东西,但我对使用API​​非常陌生,并且被所有文本淹没了。感谢任何帮助!

解决方案

您是否在寻找这个:

 library(ggmap)
from< - 'Paris'
to< - 'London'
mapdist(from,to,mode ='driving' )
至m公里英里秒分钟小时
1巴黎伦敦454416 454.416 282.3741 18283 304.7167 5.078611

mapdist 使用 Google地图计算地图距离。 要回答你的问题,我认为使用json版本的google API比使用XML更容易(甚至推荐)。

这是一个使用 RJSONIO 的快速版本。即使我建议你使用上面的功能。

  library(RJSONIO)
distHoras < - 函数(原点,目标){

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

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


mode4url < - paste(mode =,'driving',sep =)
lang4url < - paste(language =,'en-EN' ,传感器=,tolower(as.character(FALSE)),
sep =)
posturl< - paste()原始地址,目标地址,mode4url,sensor4url,
sep =&)
url_string< - paste(http://maps.googleapis.com/maps/api/distancematrix/json?,
(paste(readLines(connect),collapse = url));}}
url_string< - URLencode(url_string)
连接< -url(url_string)
树< - fromJSON ))
close(connect)
rapply(tree $ rows,I)
}

现在您测试它:

  distHoras('Paris','London')
elements.distance.text elements.distance.value elements.duration.text
454 km4544165小时5分钟
elements.duration.value elements.status
18283OK


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

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!

解决方案

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.

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

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.

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" 

这篇关于如何从Google Maps API获取驾车时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆