The package computes distances and travel times between points using Google Maps, with two functions:
distance_routes() uses the Routes
API, Google’s current API for this purpose. It computes distances
and times for pairs of points (first origin to first destination, second
to second, and so on) and returns a data frame with one row per pair.
Modes of transportation are DRIVE, WALK,
BICYCLE, TRANSIT, TWO_WHEELER and
TRUCK.gmapsdistance() uses the Distance
Matrix API, and can also compute full matrices of all origins
against all destinations. The Distance Matrix API is now legacy: since
October 2026 it can no longer be enabled for new Google Cloud projects.
gmapsdistance() is kept for backward compatibility.Distances are returned in meters and times in seconds.
An API
key is necessary to perform the queries, with the Routes API (for
distance_routes()) or the Distance Matrix API (for
gmapsdistance()) enabled. Google Maps must be able to find
both the origin and the destination of a trip.
While the R package is open source, the Google Maps APIs are a commercial service requiring registration in all cases. Google provides a free monthly allowance - e.g. 10,000 basic Routes API requests (each origin - destination pair counts as one) - which is sufficient for most R package users; requests considering traffic, and those for trucks and two-wheelers, have smaller allowances. See Google Maps Platform pricing for details.
Also note that using the API is subject to Google Maps Platform Terms of Service.
# CRAN install / stable version
install.packages("gmapsdistance")
# Github installation / current dev version
remotes::install_github("jlacko/gmapsdistance")In this example we compute the driving distance between Washington DC
and New York City using the Routes API. The result is a data frame with
one row per origin - destination pair; duration_base is the
travel time without traffic and duration_traffic the time
in traffic (the two are equal unless traffic is considered).
results <- distance_routes(origin = "Washington DC",
destination = "New York City NY",
mode = "DRIVE",
key = Sys.getenv("GOOGLE_API_KEY")) # your actual API key comes here...
results
# origin destination mode status distance_text distance_value duration_base duration_traffic
# 1 Washington DC New York City NY DRIVE OK 362 km 361969 14096 14096Several pairs can be computed in one call, optionally with a
departure time (as POSIXct, with an explicit time zone) and
a traffic model:
results <- distance_routes(origin = c("Washington DC", "New York NY"),
destination = c("Los Angeles CA", "Austin TX"),
mode = "DRIVE",
departure = as.POSIXct("2026-11-14 23:13:20", tz = "America/New_York"),
traffic_model = "PESSIMISTIC",
key = Sys.getenv("GOOGLE_API_KEY"))
results[, c("origin", "destination", "distance_value", "duration_base", "duration_traffic")]
# origin destination distance_value duration_base duration_traffic
# 1 Washington DC Los Angeles CA 4295051 140529 158915
# 2 New York NY Austin TX 2803898 93205 103660An example of legacy function gmapsdistance() to
generate a distance matrix is:
results <- gmapsdistance(origin = c("Washington DC", "New York NY", "Seattle WA", "Miami FL"),
destination = c("Washington DC", "New York NY", "Seattle WA", "Miami FL"),
mode = "bicycling",
key = Sys.getenv("GOOGLE_API_KEY")) # your actual API key comes here...
results$DistanceMore examples are in the package vignette.
The number and size of requests are limited by Google; see the limits of the Routes API and of the Distance Matrix API.
GNU General Public License v3.0
We encourage any kind of suggestions to improve the quality of this code. You can submit pull requests indicating clearly what is the purpose of the change and why we should accept such pull request. Although not necessary, we encourage you to verify that your suggestions are in accordance with the general guidelines established in the CRAN repository by running the R CMD check command.
Please see the file CODE_OF_CONDUCT.md for the Code of Conduct for the Contributor Covenant Code of Conduct.
This code was developed originally by Rodrigo Azuero and David Zarruk.
It is currently maintained by Jindra Lacko.
AUTHORS.md has a list of everyone who has contributed to gmapsdistance.
We like to keep track of the projects where gmapsdistance has been used. This will help us identify how to better improve the code. Let us know if you use gmapsdistance! Below you will find links to some of the projects and some of the references to gmapsdistance that we have found.
Proximity to pediatric cardiac specialty care for adolescents with congenital heart defects. Link to article.
Measuring Accessibility to Rail Transit Stations in Scarborough: Subway vs. LRT. Link to article
Social Data Science Course. University of Copenhagen. Department of Economics. Link
R-bloggers. The collaborative innovation landscape in data science. Link
RPubs. Link
Identifying Partnership Opportunities at Air Force Installations: A Geographic Information Systems Approach Link
DataWookie. Review of gmapsdistance. Link
The Role of Telehealth in Promoting Equitable Abortion Access in the United States: Spatial Analysis Link to article
The relationship between living in urban and rural areas of Scotland and children’s physical activity and sedentary levels: a country-wide cross-sectional analysis Link to article
E-trikes for urban delivery: An empirical mixed-fleet simulation approach to assess city logistics sustainability Link to article