gmapsdistance

R-CMD-check Downloads-monthly Downloads-total License CRAN_Status_Badge CRAN-checks Codecov test coverage

Interface Between R and Google Maps

The package computes distances and travel times between points using Google Maps, with two functions:

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.

Installation

# CRAN install / stable version 
install.packages("gmapsdistance")

# Github installation / current dev version
remotes::install_github("jlacko/gmapsdistance")

Examples of use

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            14096

Several 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           103660

An 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$Distance

More examples are in the package vignette.

Usage limits

The number and size of requests are limited by Google; see the limits of the Routes API and of the Distance Matrix API.

License

GNU General Public License v3.0

How to contribute

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.

Code of conduct

Please see the file CODE_OF_CONDUCT.md for the Code of Conduct for the Contributor Covenant Code of Conduct.

Authors

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.

Where has gmapsdistance been used or mentioned

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.

  1. Proximity to pediatric cardiac specialty care for adolescents with congenital heart defects. Link to article.

  2. Measuring Accessibility to Rail Transit Stations in Scarborough: Subway vs. LRT. Link to article

  3. Social Data Science Course. University of Copenhagen. Department of Economics. Link

  4. R-bloggers. The collaborative innovation landscape in data science. Link

  5. RPubs. Link

  6. Identifying Partnership Opportunities at Air Force Installations: A Geographic Information Systems Approach Link

  7. DataWookie. Review of gmapsdistance. Link

  8. The Role of Telehealth in Promoting Equitable Abortion Access in the United States: Spatial Analysis Link to article

  9. 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

  10. E-trikes for urban delivery: An empirical mixed-fleet simulation approach to assess city logistics sustainability Link to article