1

Have an R script to run a bunch of different points for commute time and distance. Have a google API key with an enabled billing account, go to run and get shut down at line 262 every time.

Tried restructuring the code. Refreshed the API Set the API key in different parts

Code works perfect up to that point, and it is making a connection with google as it is showing in the API.

emp_commute$CommuteTime[i] <- gmapsdistance(origin = emp_commute$HomeComplete[i],
                                            destination = emp_commute$WorkComplete[i],
                                            mode = "driving",
                                            key = "",
                                            arr_date = "2019-11-13",
                                            arr_time = emp_commute$ArrivalTime[i])$Time[1]

Error in gmapsdistance(origin = emp_commute$HomeComplete[i], destination = emp_commute$WorkComplete[i], : Google API returned an error: You must use an API key to authenticate each request to Google Maps Platform APIs. For additional information, please refer to http://g.co/dev/maps-no-account

Googled and googled, just would love some advice!

evan
  • 5,443
  • 2
  • 11
  • 20
  • What package is `gmapsdistance` from? Without seeing the rest of your code and how you are setting the API key, it is very. difficult to provide meaningful advice. – Dave2e Aug 08 '19 at 23:33
  • Hey Dave2e. Thanks for the reply. The package is the gmapsdostance package found here: https://cran.r-project.org/web/packages/gmapsdistance/readme/README.html. I’m setting the key in the fifth or so line down, where it says”key=“””. The rest of the code is only setting up a data frame with two columns of addresses that appear to work just fine. It just errors at line 262 everything and says there’s no billing account... – Bryce Butzine Aug 10 '19 at 16:05
  • I am not familiar with this package. The maybe issues with your Google API key, you can try logging onto Google to verify if the requests were being accepted. Another option is to try the "ggmap" package, it should have a similar function and there are more examples here at Stackoverflow for that package. – Dave2e Aug 10 '19 at 19:19
  • 1
    Did you set your API key with `#set.api.key("key")`? Does the exact code in [example #5](https://cran.r-project.org/web/packages/gmapsdistance/readme/README.html#example-5) and/or [example #7](https://cran.r-project.org/web/packages/gmapsdistance/readme/README.html#example-7) work with your API key? Also try making a Distance Matrix call directly from a browser tab to ensure your API key is working. E.g.: https://maps.googleapis.com/maps/api/distancematrix/json?origins=Boston,MA|Charlestown,MA&destinations=Lexington,MA|Concord,MA&departure_time=now&key=YOUR_API_KEY – evan Aug 11 '19 at 08:05
  • Thank you very much for that website! I didn’t know that was a thing. I’ve tried the set.api.key=“”, both inside the distance matrix function and outside of it, with no luck. Really appreciate the reply! – Bryce Butzine Aug 11 '19 at 20:02
  • Okay so I've just tested this package myself and it's working fine with my own API key. Gonna post my code in an answer below – evan Aug 18 '19 at 07:46

2 Answers2

0

This package works without problem for me. Used RStudio Cloud. Here's what I did.

First I installed gmapsdistance:

install.packages("gmapsdistance")

Then I ran the following code with my API key (set in the key= parameter):

library("gmapsdistance")

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="driving", key="abcd", arr_date="2019-11-13", arr_time="09:00:00")

results

The response was:

$Time
                      or Time.43.0995629+-79.0437609 Time.41.7096483+-86.9093986
1  40.431478+-80.0505401                       13878                       23071
2 33.7678359+-84.4906438                       49402                       38351

[...]

Using set.api.key("") also worked fine. For testing purposes I recommend that you try my exact steps and code above. Also double check the following:

  1. Billing and the Distance Matrix API are enabled on your project (go through Google's guide)
  2. Your values for origin, destination and arr_time are valid (try hard-coding them first)

Hope this helps.

evan
  • 5,443
  • 2
  • 11
  • 20
0

I found the issue: It is when there is a # in the address (e.g., 123 Main St Apt #6).

If you remove the :Apt #6", so it is just "123 Main St", then it works fine.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
T_R
  • 33
  • 3