Problem with ggmap - mapdist() function

I have this code. I have my Google API set up already, registered as well in R, Distance Matrix API has been initiated as well in the Google Cloud console.

Here is the dataframe I have, random 25 postal codes FROM and TO postal codes.

Dataset_test = data.frame(
       FROM_POSTAL = c("V8A 0E5","T4G 6M4","V1N 8X3",
                       "C1B 5G1","R5H 2L4","H9S 8L4","L8E 4Y0","H2Y 7N6",
                       "K1B 7C0","G4A 5B0","E4P 3T2","E4V 5P4","H3J 1R5",
                       "G0B 4J7","E7A 6E7","E5B 2Y9","S4H 1T8","A2V 4G5",
                       "V8L 2A9","T9E 1M5","A5A 5M2","E4T 5B4","S2V 6C4",
                       "S9H 5P8","B1Y 0V0"),
         TO_POSTAL = c("G0J 0B8","N0H 9N4","J9B 4Y4",
                       "L3Z 2Y7","E8K 4R4","B4P 7X9","S4H 2M0","A1Y 0B8",
                       "A1W 1E9","P9N 7X1","E4R 4B0","N0P 0M8","E1W 9Y7",
                       "T9W 8E2","G6X 4S9","A0E 0V4","J5X 7N8","N4N 8A1",
                       "V9K 0B9","L4G 3H7","E1W 0T2","G5R 9G3","L7C 9S2",
                       "E8P 2X6","E2A 2M1")
)

Here is the simple script I have to try to calculate the distance between the two postal codes by driving using Google's Distance Matrix API.

Driving_Distance = mapdist(from = Dataset_test$FROM_POSTAL, to = Dataset_test$TO_POSTAL, mode = c("driving")) 

When I run it, it throws an error in the Driving_Distance - says:

Error: Argument 1 is a list, must contain atomic vectors

Any help would be great...!

Hi @PeterHui,
Those postcodes look like they are in the UK. I also could not get them to work correctly. I searched and found that the Google maps API will not accept UK postcodes (due to licensing costs from Royal Mail). You will need to convert those postcodes into actual addresses and this will likely not be free.

> mapdist("V8A0E5, UK", "G0J0B8, UK")
Source :   ...snip...

Error in vec_rbind(!!!dots, .names_to = .id) : 
  Internal error: Unexpected `NULL` in `vec_slice_impl()`.
> mapdist("London, UK", "Manchester, UK")
Source :   ...snip...

# A tibble: 1 x 9
  from       to                  m    km miles seconds minutes hours mode   
  <chr>      <chr>           <int> <dbl> <dbl>   <int>   <dbl> <dbl> <chr>  
1 London, UK Manchester, UK 335353  335.  208.   14048    234.  3.90 driving
> mapdist("London, UK", "Manchester, UK", mode="walking")
Source :  ... snip...

# A tibble: 1 x 9
  from       to                  m    km miles seconds minutes hours mode   
  <chr>      <chr>           <int> <dbl> <dbl>   <int>   <dbl> <dbl> <chr>  
1 London, UK Manchester, UK 288465  288.  179.  214773   3580.  59.7 walking
> 

HTH

2 Likes

those postal codes are sample codes, i just grabbed them from a postal code generator. I'm not exactly sure what the error means and even when I tried actual postal codes the error still persists...

thanks for the responses :slight_smile:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

Actually, I'm pretty sure these are not UK postcodes unless a new format has been released to expand the number of postcodes of which I am not aware.