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.
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.
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.
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 = 1740000000))
results
# 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 = "2025-02-19",
dep_time = "22:20:00"))
results
## Error in parse(text = input): <text>:7:48: unexpected ')'
## 6: # departure time as seconds from Unix Epoch (1970-01-01)
## 7: departure = 1740000000))
## ^
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 4296675
## 2 New York NY Los Angeles CA 4490328
## 3 Seattle WA Los Angeles CA 1828632
## 4 Miami FL Los Angeles CA 4397630
## 5 Washington DC Austin TX 2452269
## 6 New York NY Austin TX 2805854
## 7 Seattle WA Austin TX 3425983
## 8 Miami FL Austin TX 2175942
## 9 Washington DC Chicago IL 1125492
## 10 New York NY Chicago IL 1271037
## 11 Seattle WA Chicago IL 3295127
## 12 Miami FL Chicago IL 2221712
##
## $Time
## or de Time
## 1 Washington DC Los Angeles CA 139365
## 2 New York NY Los Angeles CA 146873
## 3 Seattle WA Los Angeles CA 62288
## 4 Miami FL Los Angeles CA 141327
## 5 Washington DC Austin TX 80376
## 6 New York NY Austin TX 92715
## 7 Seattle WA Austin TX 118213
## 8 Miami FL Austin TX 70492
## 9 Washington DC Chicago IL 38705
## 10 New York NY Chicago IL 43244
## 11 Seattle WA Chicago IL 107329
## 12 Miami FL Chicago IL 72543
##
## $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
This example computes the travel distance and time between two vectors of cities defined as LAT-LONG coordinates.
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 413808
## 2 33.7678359 -84.4906438 43.0995629 -79.0437609 1622043
## 3 40.431478 -80.0505401 41.7096483 -86.9093986 728258
## 4 33.7678359 -84.4906438 41.7096483 -86.9093986 1178597
##
## $Time
## or de Time
## 1 40.431478 -80.0505401 43.0995629 -79.0437609 83955
## 2 33.7678359 -84.4906438 43.0995629 -79.0437609 318208
## 3 40.431478 -80.0505401 41.7096483 -86.9093986 137364
## 4 33.7678359 -84.4906438 41.7096483 -86.9093986 232273
##
## $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
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 4985553
## 2 New York NY Austin TX 3334524
## 3 Seattle WA Chicago IL 3646872
## 4 Miami FL Philadelphia PA 2225675
##
## $Time
## or de Time
## 1 Washington DC Los Angeles CA 923241
## 2 New York NY Austin TX 630635
## 3 Seattle WA Chicago IL 684820
## 4 Miami FL Philadelphia PA 419664
##
## $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 4985553 3123602 1274617 242729
## New York NY 5196475 3334524 1502498 161241
## Seattle WA 2151831 3840545 3646872 5154454
## Miami FL 4897650 2510458 2457591 2225675
##
## $Time
## Los Angeles CA Austin TX Chicago IL Philadelphia PA
## Washington DC 923241 588824 245668 48670
## New York NY 965052 630635 291272 32101
## Seattle WA 419898 718781 684820 974299
## Miami FL 899330 460604 471343 419664
##
## $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 4985553
## 2 New York NY Los Angeles CA 5196475
## 3 Seattle WA Los Angeles CA 2151831
## 4 Miami FL Los Angeles CA 4897650
## 5 Washington DC Austin TX 3123602
## 6 New York NY Austin TX 3334524
## 7 Seattle WA Austin TX 3840545
## 8 Miami FL Austin TX 2510458
## 9 Washington DC Chicago IL 1274617
## 10 New York NY Chicago IL 1502498
## 11 Seattle WA Chicago IL 3646872
## 12 Miami FL Chicago IL 2457591
## 13 Washington DC Philadelphia PA 242729
## 14 New York NY Philadelphia PA 161241
## 15 Seattle WA Philadelphia PA 5154454
## 16 Miami FL Philadelphia PA 2225675
##
## $Time
## or de Time
## 1 Washington DC Los Angeles CA 923241
## 2 New York NY Los Angeles CA 965052
## 3 Seattle WA Los Angeles CA 419898
## 4 Miami FL Los Angeles CA 899330
## 5 Washington DC Austin TX 588824
## 6 New York NY Austin TX 630635
## 7 Seattle WA Austin TX 718781
## 8 Miami FL Austin TX 460604
## 9 Washington DC Chicago IL 245668
## 10 New York NY Chicago IL 291272
## 11 Seattle WA Chicago IL 684820
## 12 Miami FL Chicago IL 471343
## 13 Washington DC Philadelphia PA 48670
## 14 New York NY Philadelphia PA 32101
## 15 Seattle WA Philadelphia PA 974299
## 16 Miami FL Philadelphia PA 419664
##
## $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
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 = "2023-11-14",
dep_time = "23:13:20",
traffic_model = "pessimistic",
shape = "long",
key=APIkey)
## Error in gmapsdistance(origin = c("Washington DC", "New York NY"), destination = c("Los Angeles CA", : The departure time has to be some time in the future!
results
## $Distance
## or de Distance
## 1 Washington DC Los Angeles CA 4985553
## 2 New York NY Los Angeles CA 5196475
## 3 Seattle WA Los Angeles CA 2151831
## 4 Miami FL Los Angeles CA 4897650
## 5 Washington DC Austin TX 3123602
## 6 New York NY Austin TX 3334524
## 7 Seattle WA Austin TX 3840545
## 8 Miami FL Austin TX 2510458
## 9 Washington DC Chicago IL 1274617
## 10 New York NY Chicago IL 1502498
## 11 Seattle WA Chicago IL 3646872
## 12 Miami FL Chicago IL 2457591
## 13 Washington DC Philadelphia PA 242729
## 14 New York NY Philadelphia PA 161241
## 15 Seattle WA Philadelphia PA 5154454
## 16 Miami FL Philadelphia PA 2225675
##
## $Time
## or de Time
## 1 Washington DC Los Angeles CA 923241
## 2 New York NY Los Angeles CA 965052
## 3 Seattle WA Los Angeles CA 419898
## 4 Miami FL Los Angeles CA 899330
## 5 Washington DC Austin TX 588824
## 6 New York NY Austin TX 630635
## 7 Seattle WA Austin TX 718781
## 8 Miami FL Austin TX 460604
## 9 Washington DC Chicago IL 245668
## 10 New York NY Chicago IL 291272
## 11 Seattle WA Chicago IL 684820
## 12 Miami FL Chicago IL 471343
## 13 Washington DC Philadelphia PA 48670
## 14 New York NY Philadelphia PA 32101
## 15 Seattle WA Philadelphia PA 974299
## 16 Miami FL Philadelphia PA 419664
##
## $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
# 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 4309328 2464927
## New York NY 4607491 2800575
##
## $Time
## Los Angeles CA Austin TX
## Washington DC 140087 81085
## New York NY 151893 93222
##
## $Status
## Los Angeles CA Austin TX
## Washington DC "OK" "OK"
## New York NY "OK" "OK"
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.