Mleinss
Mleinss

Reputation: 37

ggmap: empty data frame with trek

I've installed the latest version of ggmap from the github repository using the following code:

devtools::install_github("dkahle/ggmap", ref = "tidyup")

I've enable the "Geocoding API" option, enabled billing and provided my Google API key. I then tried to rerun a code using the trek function and plot the path on my map with geom_path. This code was working a few months ago (June 2018), but now I get the error:

Error in FUN(X[[i]], ...) : object 'lon' not found

I then tried to run the example in the trek vignette and got the same error message. So, using the example in the vignette:

from <- "houston, texas"
to <- "waco, texas"
trek_df <- trek(from, to, structure = "route")
qmap("college station, texas", zoom = 8) +
  geom_path(
    aes(x = lon, y = lat),  colour = "blue",
    size = 1.5, alpha = .5,
    data = trek_df, lineend = "round"
  )

Error in FUN(X[[i]], ...) : object 'lon' not found

It seems that the problem arises when the function trek is called. It should give a data frame (output="simple") or all of the geocoded information (output="all"), but there is an empty dataframe:

> trek_df
# A tibble: 0 x 0

Is there something more I have to do with Google to enable this function to work? Thanks for your help.

Upvotes: 1

Views: 125

Answers (1)

Roman
Roman

Reputation: 4989

The most probable reason is that you have not enabled the Directions API in the Google Console.

Explanation

trek() is calling the Directions API as you can see here:

> trek_df <- trek(from, to, structure = "route")
Source : https://maps.googleapis.com/maps/api/directions/json?origin=houston%2C%20texas&destination=waco%2C%20texas&mode=driving&units=metric&alternatives=false&key=xxx

qmap() is calling (in that case) the Static and Geocode APIs:

> qmap("college station, texas", zoom = 8) +
+     geom_path(
+         aes(x = lon, y = lat),  colour = "blue",
+         size = 1.5, alpha = .5,
+         data = trek_df, lineend = "round"
+     )
Source : https://maps.googleapis.com/maps/api/staticmap?center=college%20station,%20texas&zoom=8&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx
Source : https://maps.googleapis.com/maps/api/geocode/json?address=college%20station%2C%20texas&key=xxx

1

Upvotes: 1

Related Questions