问题描述
我有一个邮政编码列表,我想找到它们相关的经度和纬度,以便将它们绘制在地图上。例如,我在下面展示了几个邮政编码:
code <- c("V6G 1X5","V6J 2A4","V5V 3N2")
现在,我想使用 ggmap
函数找到每个邮政编码的经度和纬度。我该怎么做?
解决方法
用ggmap::geocode()
。请注意,您可能需要先注册 Google 地图 API 密钥。
见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")