1

I have a list of postal codes and I want to find their associated longitude and latitude in order to plot them on a map. For example, I showed couple of postal codes below:

code <- c("V6G 1X5", "V6J 2A4", "V5V 3N2")

Now, I want to find longitude and latitude for each of these postal code using ggmap function. How can I do this?

Ross_you
  • 881
  • 5
  • 22
  • 1
    https://stackoverflow.com/questions/66486328/r-language-convert-canadian-postal-code-to-longitude-and-latitude – Ben Bolker Apr 19 '21 at 00:39

1 Answers1

3

With ggmap::geocode(). Note that you might need to register you Google maps API key first. See https://rdrr.io/cran/ggmap/man/register_google.html

library(ggmap)
library(tidyverse)
#register_google(key = "your_api_key_here") 

code <- c("V6G 1X5", "V6J 2A4", "V5V 3N2")

df_points <- ggmap::geocode(location = code)
map_vancouver <- get_googlemap(center = "Vancoucer, BC, Canada", zoom = 13)

ggmap(map_vancouver) +  geom_point(data = df_points, aes(x = lon, y = lat), colour = "red")

enter image description here

Nicolás Velasquez
  • 5,623
  • 11
  • 22
  • 1
    huh, I didn't know `ggmap` did reverse geocoding by canadian postal code. Nice. My linked answer would get you reverse geocoding slightly less conveniently, but you could do it locally at any volume you wanted without accessing Google services. – Ben Bolker Apr 19 '21 at 01:31
  • @BenBolker thanks so much. The link you provided actually worked for me and it was pretty much straight forward to use the solution proposed there – Ross_you Apr 19 '21 at 01:44
  • @Nicolás Velásquez thanks so much for your solution. This is amazing and I can use ggmap to find the location for all my points. Thanks – Ross_you Apr 19 '21 at 01:45