Yijiao Liu
Yijiao Liu

Reputation: 304

How to get zip code dataset by ggmap package in R

By using geocode('Zip') I can get the location coordinates. What about given the coordinate, can I get the zip code of that location? For example

geocode('130022,长春')

which gives me the coordinate of my hometown. 130022 is the zip code, 长春 is the city. By doing this I want to get the unique geo coordinates in 长春 city, which is (125.35, 43.86364). What if I give the coordinate, how I can get the zip code data back?

Upvotes: 1

Views: 1000

Answers (1)

SymbolixAU
SymbolixAU

Reputation: 26248

use revgeocode()

revgeocode(location = c(125.35, 43.8634))
Information from URL : http://maps.googleapis.com/maps/api/geocode/json?latlng=43.8634,125.35&sensor=false
[1] "Jun Zhuan Hu Tong, Nanguan Qu, Changchun Shi, Jilin Sheng, China, 130022"

And if you want more detail, specify output = 'all'

res <- revgeocode(location = c(125.35, 43.8634), output = 'all')

str(res)
# List of 2
# $ results:List of 6
# ..$ :List of 5
# .. ..$ address_components:List of 6
# .. .. ..$ :List of 3
# .. .. .. ..$ long_name : chr "Jun Zhuan Hu Tong"
# .. .. .. ..$ short_name: chr "Jun Zhuan Hu Tong"
# .. .. .. ..$ types     : chr "route"
# .. .. ..$ :List of 3
# .. .. .. ..$ long_name : chr "Nanguan Qu"
# .. .. .. ..$ short_name: chr "Nanguan Qu"
# .. .. .. ..$ types     : chr [1:3] "political" "sublocality" "sublocality_level_1"
# .. .. ..$ :List of 3
# .. .. .. ..$ long_name : chr "Changchun Shi"
# .. .. .. ..$ short_name: chr "Changchun Shi"
# .. .. .. ..$ types     : chr [1:2] "locality" "political"
# .. .. ..$ :List of 3
# .. .. .. ..$ long_name : chr "Jilin Sheng"
# .. .. .. ..$ short_name: chr "Jilin Sheng"
# .. .. .. ..$ types     : chr [1:2] "administrative_area_level_1" "political"
# ... etc

Upvotes: 2

Related Questions