You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于R与Google Map API的城市研究路线拥堵数据获取问询

Solution for Fetching Route Congestion Data via Google Maps API in R

Hey there! For your urban research needs, you absolutely can get the congestion data for alternative routes over a specific time window using Google Maps' official APIs—and the googleway package you're already using can actually do this, you just need to target the right endpoint and parameters. Let me walk you through how:

1. Which Google API to Use?

The Google Maps Directions API is exactly what you need here. It returns detailed route information including:

  • Multiple alternative routes (when enabled)
  • Travel duration with traffic (real-time or historical, based on your specified departure time)
  • Baseline travel duration (without traffic)
  • Congestion-related delays calculated as the difference between these two durations

2. Using googleway to Fetch the Data

You were on the right track with googleway—it has built-in support for the Directions API. The key parameters you missed are:

  • alternatives = TRUE: Forces the API to return all viable alternative routes between your two points
  • departure_time: A Unix timestamp (in seconds) specifying when you want to simulate departure (this works for both real-time traffic and historical traffic data—just use a timestamp from your target time window)
  • traffic_model: Optional, but useful for historical data; use best_guess (default) or specify pessimistic/optimistic if you want to model different congestion scenarios

Example Code

Here’s a quick snippet to get you started:

library(googleway)

# Set your Google API key (make sure Directions API is enabled in Google Cloud Console)
set_key("YOUR_GOOGLE_API_KEY")

# Define your origin, destination, and target departure time (Unix timestamp)
origin <- "Times Square, New York"
destination <- "Brooklyn Bridge, New York"
# Example: Departure time of 9 AM on October 1, 2024 (convert to Unix seconds)
departure_time <- as.integer(as.POSIXct("2024-10-01 09:00:00", tz = "America/New_York"))

# Call the Directions API with alternatives and traffic data
route_data <- google_directions(
  origin = origin,
  destination = destination,
  departure_time = departure_time,
  alternatives = TRUE,
  traffic_model = "best_guess"
)

# Extract congestion data for each alternative route
if (route_data$status == "OK") {
  routes <- route_data$routes
  
  for (i in seq_along(routes)) {
    route <- routes[[i]]
    leg <- route$legs[[1]]
    
    # Baseline duration (without traffic)
    base_duration <- leg$duration$value / 60  # Convert seconds to minutes
    # Duration with traffic
    traffic_duration <- leg$duration_in_traffic$value / 60
    # Congestion delay
    congestion_delay <- traffic_duration - base_duration
    
    cat(sprintf("Route %d:\n", i))
    cat(sprintf("  Base duration: %.1f mins\n", base_duration))
    cat(sprintf("  Duration with traffic: %.1f mins\n", traffic_duration))
    cat(sprintf("  Congestion delay: %.1f mins\n\n", congestion_delay))
  }
} else {
  cat("Error fetching data:", route_data$status)
}

3. Key Notes for Your Research

  • Historical Traffic Data: Google Directions API provides historical traffic data for timestamps up to several months in the past—perfect for analyzing time-window congestion patterns.
  • API Quotas & Costs: Make sure to check Google Cloud's pricing for Directions API calls; historical traffic requests may have the same cost as real-time ones, but verify your quota limits to avoid unexpected charges.
  • Data Parsing: The response is a nested list, so you can use packages like purrr or jsonlite to streamline extracting metrics across multiple routes or time points for your analysis.

4. Alternative Package (If You Want More Options)

If you want another R package to work with, gmapsdistance also supports calling the Directions API and can return traffic-aware duration data. But googleway is more flexible for map visualization and data extraction once you know the right parameters.

内容的提问来源于stack exchange,提问作者mjoudy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:20:08