【问题标题】:How do I get driving time from Google Maps API?如何从 Google Maps API 获取驾驶时间?
【发布时间】:2013-06-21 08:27:20
【问题描述】:

假设平均速度为 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)
}

我如何调整这个函数以便让它直接产生时间,所以我不需要做出这个 65 公里/小时的假设,从而得到更好的估计?阅读the documentation 后,我尝试用“持续时间”切换“距离”,但没有奏效。我可能遗漏了一些简单的东西,但我对使用 API 还是很陌生,并且对所有这些文本感到不知所措。感谢任何帮助!

【问题讨论】:

    标签: xml r google-maps-api-3 driving-distance


    【解决方案1】:

    你在找这个吗:

    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 使用 Google 地图 计算地图距离。

    为了回答您的问题,我认为使用 json 版本的 google API 比使用 XML 版本更容易(甚至推荐)。

    这里是使用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)
    }
    

    现在你测试一下:

    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" 
    

    【讨论】:

    • 酷,我不知道有这个功能。像魅力一样工作,谢谢!尽管如此,我仍然想知道如何调整我提供的手工制作的功能。
    • @wleoncio 学习 mapdist 代码是一个很好的练习。您可以查看我对小版本的编辑。
    • 感谢您的提示,我很快就会投入其中。
    【解决方案2】:

    我将把我自己的包加入到这个组合中,它也会为你查询 Google 的 API

    (您需要一个有效的 Google 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"
    

    【讨论】:

      猜你喜欢
      • 2012-08-31
      • 1970-01-01
      • 2013-05-22
      • 2015-05-14
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      相关资源
      最近更新 更多