gmapsdistance package in use

Jindra Lacko

2022-04-26

Setting up API key

The Google Distance Matrix API requires registration, and an API key is required in each function call. It is possible to store the key for duration of a R session by using gmapsdistance::set.api.key() call.

Note that in order to keep the maintainers API key private it has been stored locally in an environment variable; in the most frequent use cases it can be used in plain text.

library(gmapsdistance)
set.api.key(Sys.getenv("GOOGLE_API_KEY"))

Distance between cities

In this example we will compute the driving distance between Washington DC, and New York City. The code returns the Time, the Distance and the Status of the query (OK if it was successful). The calculation will be made using the “driving” method, which is the default.

results = gmapsdistance(origin = "Washington DC",
                        destination = "New York City NY")
results
## $Distance
## [1] 361947
## 
## $Time
## [1] 14145
## 
## $Status
## [1] "OK"

Walking distance between GPS coordinates

In this example we will compute the driving distance between the Greek cities of Marathon and Athens. We show that the function is able to handle LAT-LONG coordinates.

results = gmapsdistance(origin = "38.1621328 24.0029257",
                        destination = "37.9908372 23.7383394",
                        mode = "walking")
results
## $Distance
## [1] 39459
## 
## $Time
## [1] 30024
## 
## $Status
## [1] "OK"

Distance matrix at a specific departure time

This example computes the travel distance and time matrices between two vectors of cities at a specific departure time

results = gmapsdistance(origin = c("Washington DC", "New York NY",
                                   "Seattle WA", "Miami FL"),
                        destination = c("Los Angeles CA", "Austin TX",
                                        "Chicago IL", "Philadelphia PA"),
                        mode = "bicycling",
                        # departure time as seconds from Unix Epoch (1970-01-01)
                        departure = 1660682400)
results
## $Distance
##               Los Angeles CA Austin TX Chicago IL Philadelphia PA
## Washington DC        4522001   2564719    1247092          229701
## New York NY          4863939   3155049    1472649          159373
## Seattle WA           1970574   3605562    3509269         4842242
## Miami FL             4548054   2316043    2354834         2139123
## 
## $Time
##               Los Angeles CA Austin TX Chicago IL Philadelphia PA
## Washington DC         843375    477113     243025           45679
## New York NY           910603    601417     291156           31515
## Seattle WA            366893    662544     650403          912709
## Miami FL              824214    420898     438438          396648
## 
## $Status
##               Los Angeles CA Austin TX Chicago IL Philadelphia PA
## Washington DC "OK"           "OK"      "OK"       "OK"           
## New York NY   "OK"           "OK"      "OK"       "OK"           
## Seattle WA    "OK"           "OK"      "OK"       "OK"           
## Miami FL      "OK"           "OK"      "OK"       "OK"
# Equivalently
results = gmapsdistance(origin = c("Washington DC", "New York NY",
                                   "Seattle WA", "Miami FL"),
                        destination = c("Los Angeles CA", "Austin TX",
                                        "Chicago IL", "Philadelphia PA"),
                        mode = "bicycling",
                        dep_date = "2022-08-16",
                        dep_time = "20:40:00")

results
## $Distance
##               Los Angeles CA Austin TX Chicago IL Philadelphia PA
## Washington DC        4522001   2564719    1247092          229701
## New York NY          4863939   3155049    1472649          159373
## Seattle WA           1970574   3605562    3509269         4842242
## Miami FL             4548054   2316043    2354834         2139123
## 
## $Time
##               Los Angeles CA Austin TX Chicago IL Philadelphia PA
## Washington DC         843375    477113     243025           45679
## New York NY           910603    601417     291156           31515
## Seattle WA            366893    662544     650403          912709
## Miami FL              824214    420898     438438          396648
## 
## $Status
##               Los Angeles CA Austin TX Chicago IL Philadelphia PA
## Washington DC "OK"           "OK"      "OK"       "OK"           
## New York NY   "OK"           "OK"      "OK"       "OK"           
## Seattle WA    "OK"           "OK"      "OK"       "OK"           
## Miami FL      "OK"           "OK"      "OK"       "OK"

Long format of distance matrix

This example computes the travel distance and time matrices between two vectors of cities and return the results in long format

origin = c("Washington DC", "New York NY", "Seattle WA", "Miami FL")
destination = c("Los Angeles CA", "Austin TX", "Chicago IL")

results = gmapsdistance(origin,
                        destination,
                        mode = "driving",
                        shape = "long")

results
## $Distance
##               or             de Distance
## 1  Washington DC Los Angeles CA  4294771
## 2    New York NY Los Angeles CA  4488869
## 3     Seattle WA Los Angeles CA  1829079
## 4       Miami FL Los Angeles CA  4395889
## 5  Washington DC      Austin TX  2450830
## 6    New York NY      Austin TX  2802946
## 7     Seattle WA      Austin TX  3410101
## 8       Miami FL      Austin TX  2169394
## 9  Washington DC     Chicago IL  1126224
## 10   New York NY     Chicago IL  1271482
## 11    Seattle WA     Chicago IL  3287320
## 12      Miami FL     Chicago IL  2224611
## 
## $Time
##               or             de   Time
## 1  Washington DC Los Angeles CA 141533
## 2    New York NY Los Angeles CA 149725
## 3     Seattle WA Los Angeles CA  63629
## 4       Miami FL Los Angeles CA 143548
## 5  Washington DC      Austin TX  81991
## 6    New York NY      Austin TX  94615
## 7     Seattle WA      Austin TX 118530
## 8       Miami FL      Austin TX  71963
## 9  Washington DC     Chicago IL  39063
## 10   New York NY     Chicago IL  44074
## 11    Seattle WA     Chicago IL 109203
## 12      Miami FL     Chicago IL  72676
## 
## $Status
##               or             de status
## 1  Washington DC Los Angeles CA     OK
## 2    New York NY Los Angeles CA     OK
## 3     Seattle WA Los Angeles CA     OK
## 4       Miami FL Los Angeles CA     OK
## 5  Washington DC      Austin TX     OK
## 6    New York NY      Austin TX     OK
## 7     Seattle WA      Austin TX     OK
## 8       Miami FL      Austin TX     OK
## 9  Washington DC     Chicago IL     OK
## 10   New York NY     Chicago IL     OK
## 11    Seattle WA     Chicago IL     OK
## 12      Miami FL     Chicago IL     OK

Distance matrix between GPS coordinates

This example computes the travel distance and time between two vectors of cities. This example shows that the function is able to handle LAT-LONG coordinates and a Google Maps API Key

origin = c("40.431478 -80.0505401", "33.7678359 -84.4906438")
destination = c("43.0995629 -79.0437609", "41.7096483 -86.9093986")

results = gmapsdistance(origin,
                        destination,
                        mode="bicycling",
                        shape="long")

results
## $Distance
##                       or                     de Distance
## 1  40.431478 -80.0505401 43.0995629 -79.0437609   418445
## 2 33.7678359 -84.4906438 43.0995629 -79.0437609  1529807
## 3  40.431478 -80.0505401 41.7096483 -86.9093986   702490
## 4 33.7678359 -84.4906438 41.7096483 -86.9093986  1112200
## 
## $Time
##                       or                     de   Time
## 1  40.431478 -80.0505401 43.0995629 -79.0437609  78872
## 2 33.7678359 -84.4906438 43.0995629 -79.0437609 290087
## 3  40.431478 -80.0505401 41.7096483 -86.9093986 131837
## 4 33.7678359 -84.4906438 41.7096483 -86.9093986 209147
## 
## $Status
##                       or                     de status
## 1  40.431478 -80.0505401 43.0995629 -79.0437609     OK
## 2 33.7678359 -84.4906438 43.0995629 -79.0437609     OK
## 3  40.431478 -80.0505401 41.7096483 -86.9093986     OK
## 4 33.7678359 -84.4906438 41.7096483 -86.9093986     OK

Combination of distances

This example computes the travel distance and time between two vectors of cities using the ‘combinations’ option.

# 1. Pairwise

or = c("Washington DC", "New York NY", "Seattle WA", "Miami FL")
des = c("Los Angeles CA", "Austin TX", "Chicago IL", "Philadelphia PA")

results = gmapsdistance(origin = or,
                        destination = des,
                        mode = "bicycling",
                        combinations = "pairwise")
results
## $Distance
##              or              de Distance
## 1 Washington DC  Los Angeles CA  4522001
## 2   New York NY       Austin TX  3155049
## 3    Seattle WA      Chicago IL  3509269
## 4      Miami FL Philadelphia PA  2139123
## 
## $Time
##              or              de   Time
## 1 Washington DC  Los Angeles CA 843375
## 2   New York NY       Austin TX 601417
## 3    Seattle WA      Chicago IL 650403
## 4      Miami FL Philadelphia PA 396648
## 
## $Status
##              or              de status
## 1 Washington DC  Los Angeles CA     OK
## 2   New York NY       Austin TX     OK
## 3    Seattle WA      Chicago IL     OK
## 4      Miami FL Philadelphia PA     OK

# 2. All combinations of origins and destinations in wide format
results = gmapsdistance(origin = or,
                        destination = des,
                        mode = "bicycling",
                        combinations = "all",
                        shape = "wide")
results
## $Distance
##               Los Angeles CA Austin TX Chicago IL Philadelphia PA
## Washington DC        4522001   2564719    1247092          229701
## New York NY          4863939   3155049    1472649          159373
## Seattle WA           1970574   3605562    3509269         4842242
## Miami FL             4548054   2316043    2354834         2139123
## 
## $Time
##               Los Angeles CA Austin TX Chicago IL Philadelphia PA
## Washington DC         843375    477113     243025           45679
## New York NY           910603    601417     291156           31515
## Seattle WA            366893    662544     650403          912709
## Miami FL              824214    420898     438438          396648
## 
## $Status
##               Los Angeles CA Austin TX Chicago IL Philadelphia PA
## Washington DC "OK"           "OK"      "OK"       "OK"           
## New York NY   "OK"           "OK"      "OK"       "OK"           
## Seattle WA    "OK"           "OK"      "OK"       "OK"           
## Miami FL      "OK"           "OK"      "OK"       "OK"

results = gmapsdistance(origin = or,
                        destination = des,
                        mode = "bicycling",
                        combinations = "all",
                        shape = "long")
results
## $Distance
##               or              de Distance
## 1  Washington DC  Los Angeles CA  4522001
## 2    New York NY  Los Angeles CA  4863939
## 3     Seattle WA  Los Angeles CA  1970574
## 4       Miami FL  Los Angeles CA  4548054
## 5  Washington DC       Austin TX  2564719
## 6    New York NY       Austin TX  3155049
## 7     Seattle WA       Austin TX  3605562
## 8       Miami FL       Austin TX  2316043
## 9  Washington DC      Chicago IL  1247092
## 10   New York NY      Chicago IL  1472649
## 11    Seattle WA      Chicago IL  3509269
## 12      Miami FL      Chicago IL  2354834
## 13 Washington DC Philadelphia PA   229701
## 14   New York NY Philadelphia PA   159373
## 15    Seattle WA Philadelphia PA  4842242
## 16      Miami FL Philadelphia PA  2139123
## 
## $Time
##               or              de   Time
## 1  Washington DC  Los Angeles CA 843375
## 2    New York NY  Los Angeles CA 910603
## 3     Seattle WA  Los Angeles CA 366893
## 4       Miami FL  Los Angeles CA 824214
## 5  Washington DC       Austin TX 477113
## 6    New York NY       Austin TX 601417
## 7     Seattle WA       Austin TX 662544
## 8       Miami FL       Austin TX 420898
## 9  Washington DC      Chicago IL 243025
## 10   New York NY      Chicago IL 291156
## 11    Seattle WA      Chicago IL 650403
## 12      Miami FL      Chicago IL 438438
## 13 Washington DC Philadelphia PA  45679
## 14   New York NY Philadelphia PA  31515
## 15    Seattle WA Philadelphia PA 912709
## 16      Miami FL Philadelphia PA 396648
## 
## $Status
##               or              de status
## 1  Washington DC  Los Angeles CA     OK
## 2    New York NY  Los Angeles CA     OK
## 3     Seattle WA  Los Angeles CA     OK
## 4       Miami FL  Los Angeles CA     OK
## 5  Washington DC       Austin TX     OK
## 6    New York NY       Austin TX     OK
## 7     Seattle WA       Austin TX     OK
## 8       Miami FL       Austin TX     OK
## 9  Washington DC      Chicago IL     OK
## 10   New York NY      Chicago IL     OK
## 11    Seattle WA      Chicago IL     OK
## 12      Miami FL      Chicago IL     OK
## 13 Washington DC Philadelphia PA     OK
## 14   New York NY Philadelphia PA     OK
## 15    Seattle WA Philadelphia PA     OK
## 16      Miami FL Philadelphia PA     OK

Using optional parameters - traffic model and avoidance

This example show the use of traffic model and avoidance parameters, while setting the API key directly in function call. Note that in order to keep the maintainers API key private it has been stored locally in an environment variable; in the most frequent use cases it can be used in plain text.

APIkey <- Sys.getenv("GOOGLE_API_KEY") # your actual API key would go here...

# Time and distance using a 'pessimistic' traffic model.
results = gmapsdistance(origin = c("Washington DC", "New York NY"),
                        destination = c("Los Angeles CA", "Austin TX"),
                        mode = "driving",
                        dep_date = "2022-08-16",
                        dep_time = "20:40:00",
                        traffic_model = "pessimistic",
                        shape = "long",
                        key=APIkey)
results
## $Distance
##              or             de Distance
## 1 Washington DC Los Angeles CA  4294771
## 2   New York NY Los Angeles CA  4488869
## 3 Washington DC      Austin TX  2450830
## 4   New York NY      Austin TX  2802946
## 
## $Time
##              or             de   Time
## 1 Washington DC Los Angeles CA 141533
## 2   New York NY Los Angeles CA 149725
## 3 Washington DC      Austin TX  81991
## 4   New York NY      Austin TX  94615
## 
## $Status
##              or             de status
## 1 Washington DC Los Angeles CA     OK
## 2   New York NY Los Angeles CA     OK
## 3 Washington DC      Austin TX     OK
## 4   New York NY      Austin TX     OK

# Time and distance avoiding 'tolls'.
results = gmapsdistance(origin = c("Washington DC", "New York NY"),
                        destination = c("Los Angeles CA", "Austin TX"),
                        mode = "driving",
                        avoid = "tolls",
                        key=APIkey)

results
## $Distance
##               Los Angeles CA Austin TX
## Washington DC        4303247   2459306
## New York NY          4514460   2799783
## 
## $Time
##               Los Angeles CA Austin TX
## Washington DC         141940     82398
## New York NY           154004     95151
## 
## $Status
##               Los Angeles CA Austin TX
## Washington DC "OK"           "OK"     
## New York NY   "OK"           "OK"

Using “foreign” place names

It is perfectly possible to use names in non-ASCII characters; care must be however made that all characters are encoded in UTF-8 though. This may not be the obvious first choice of encoding, especially in Windows environments.

results = gmapsdistance(origin = "вулиця Хрещатик, Київ",
                        destination = "Майдан Незалежності, Київ",
                        mode = "walking")
results
## $Distance
## [1] 596
## 
## $Time
## [1] 469
## 
## $Status
## [1] "OK"