2

Two questions related to this topic. First, with the sample address '方桥社区居委会', I first checked it directly from Google Map, and I get both its location and its English translation 'Fangqiao Community Residents' Committee'. However, when I try to run the code

library(googleway)
key <- 'APIkey'
google_geocode(address = "方桥社区居委会", language = "CN", key = key)

The result I got from above Chinese address is

$results

address_components
1 Xinhai Avenue, Yaohai Qu, Hefei Shi, Anhui Sheng, China, Xinhai Ave, Yaohai 
Qu, Hefei Shi, Anhui Sheng, CN, route, political, sublocality, 
sublocality_level_1, locality, political, administrative_area_level_1, 
political, country, political
                                 formatted_address geometry.location.lat
1 Xinhai Ave, Yaohai Qu, Hefei Shi, Anhui Sheng, China              31.89561
  geometry.location.lng geometry.location_type 
geometry.viewport.northeast.lat
1              117.3393            APPROXIMATE                        
31.89696
  geometry.viewport.northeast.lng geometry.viewport.southwest.lat
1                        117.3407                        31.89426
  geometry.viewport.southwest.lng                    place_id
1                         117.338 ChIJ2attw2plyzUR782Meh0Xnvs
                         types
1 establishment, point_of_interest

$status
[1] "OK"

I can not get its postal_code information. When I use the following codes

google_geocode(address = "Fangqiao Community Residents' Committee", language = "CN", key = key)

I can get its postal_code information.

$results

address_components
1 Jiangdu Road, Guangling Qu, Yangzhou Shi, Jiangsu Sheng, China, 225009, 
Jiangdu Rd, Guangling Qu, Yangzhou Shi, Jiangsu Sheng, CN, 225009, route, 
political, sublocality, sublocality_level_1, locality, political, 
administrative_area_level_1, political, country, political, postal_code
                                                 formatted_address
1 Jiangdu Rd, Guangling Qu, Yangzhou Shi, Jiangsu Sheng, China, 225009
  geometry.location.lat geometry.location.lng geometry.location_type
1              32.39118              119.4622            APPROXIMATE
  geometry.viewport.northeast.lat geometry.viewport.northeast.lng
1                        32.39253                        119.4635
  geometry.viewport.southwest.lat geometry.viewport.southwest.lng 
partial_match
1                        32.38983                        119.4608          
TRUE
                 place_id                            types
1 ChIJcQhNY82HtjURw8QwBzgCGgA establishment, point_of_interest

$status
[1] "OK"

Why is there a difference? May I know how this package works or how Google Map works, and in which way I can get the zip code of a location of China for sure?

Yijiao Liu
  • 184
  • 12
  • 1
    What *are* you getting back from `google_geocode()` with the non-English address? The code for that function can be found here: https://github.com/SymbolixAU/googleway/blob/master/R/google_geocode.R – Jonathan Carroll Jun 16 '17 at 06:26
  • I get "Xinhai Ave, Yaohai Qu, Hefei Shi, Anhui Sheng, China" as the `formatted_address`. Entering the lat/long into Google maps returns "CPC Fangqiao Community Dang General Branch Committee" at that location. – Jonathan Carroll Jun 16 '17 at 06:30
  • I just updated the question with the results I got, but I found the address with the English translation is wrong, which belongs to a different province, Jiangsu.@JonathanCarroll – Yijiao Liu Jun 16 '17 at 06:36

2 Answers2

1

I'm not entirely certain how Google Maps stores, sorts or returns it's data. However, with your example you can use the place_id inside the google_place_details() function to get more details about the place returned from the query.

So taking the place_id that's returned from your first query you can do

library(googleway)
# key <- 'APIkey'
res <- google_geocode(address = "方桥社区居委会", language = "CN", key = key)

res_details <- google_place_details(place_id = res$results$place_id, key = key)

And in the result it you see the field res_details$result$name "Fangqiao Community Residents' Committee"

res_details
# $result
# $result$address_components
# long_name  short_name                                       types
# 1 Xinhai Avenue  Xinhai Ave                                       route
# 2     Yaohai Qu   Yaohai Qu sublocality_level_1, sublocality, political
# 3     Hefei Shi   Hefei Shi                         locality, political
# 4   Anhui Sheng Anhui Sheng      administrative_area_level_1, political
# 5         China          CN                          country, political
# 6        230000      230000                                 postal_code
# 
# $result$adr_address
# [1] "<span class=\"street-address\">Xinhai Ave</span>, <span class=\"extended-address\">Yaohai Qu</span>, <span class=\"locality\">Hefei Shi</span>, <span class=\"region\">Anhui Sheng</span>, <span class=\"country-name\">China</span>, <span class=\"postal-code\">230000</span>"
# 
# $result$formatted_address
# [1] "Xinhai Ave, Yaohai Qu, Hefei Shi, Anhui Sheng, China, 230000"
# 
# $result$geometry
# $result$geometry$location
# $result$geometry$location$lat
# [1] 31.89561
# 
# $result$geometry$location$lng
# [1] 117.3393
# 
# 
# $result$geometry$viewport
# $result$geometry$viewport$northeast
# $result$geometry$viewport$northeast$lat
# [1] 31.89763
# 
# $result$geometry$viewport$northeast$lng
# [1] 117.3409
# 
# 
# $result$geometry$viewport$southwest
# $result$geometry$viewport$southwest$lat
# [1] 31.89493
# 
# $result$geometry$viewport$southwest$lng
# [1] 117.3382
# 
# 
# 
# 
# $result$icon
# [1] "https://maps.gstatic.com/mapfiles/place_api/icons/civic_building-71.png"
# 
# $result$id
# [1] "b8765b898aed8524b89bd2923884fd832d3e0762"
# 
# $result$name
# [1] "Fangqiao Community Residents' Committee"
# 
# $result$place_id
# [1] "ChIJ2attw2plyzUR782Meh0Xnvs"
# 
# $result$reference
# [1] "CmRSAAAARkCCJDC2E73NLZr8XSRJsoiAnxk9-jOCOUULjCjaZv8yUdPcEpaz45ZZ1JpWlodJqxjfyEcgiwk2BkjoLnvKGYSoTyCFEkNrcPdG_gZepdugcMf33gtOccKFOad911fpEhCyTL_VieVWzzafFN8LFv9WGhT-I75yslbEZsOEOxFpBLSeO25zGA"
# 
# $result$scope
# [1] "GOOGLE"
# 
# $result$types
# [1] "point_of_interest" "establishment"    
# 
# $result$url
# [1] "https://maps.google.com/?cid=18130954565217734127"
# 
# $result$utc_offset
# [1] 480
# 
# $result$vicinity
# [1] "Xinhai Avenue, Yaohai, Hefei"

Is this the correct result? Maybe there's more than one Fangqiao Community Resident's Committee?

SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
0

You can consider using the geocode() function from the ggmap package.

library(ggmap)

result <- geocode("方桥社区居委会", output = 'more', source = 'google')

## components in the 'result' variable
 lon      lat          type          loctype
1 117.3393 31.89561 establishment geometric_center
                                                       address
1 xinhai ave, yaohai qu, hefei shi, anhui sheng, china, 230000
     north    south     east    west         route political
1 31.89696 31.89426 117.3407 117.338 Xinhai Avenue Yaohai Qu
   locality administrative_area_level_1 country postal_code
1 Hefei Shi                 Anhui Sheng   China      230000

To obtain the zip code / postal code:

> result$postal_code
[1] 230000
jacky_learns_to_code
  • 824
  • 3
  • 11
  • 29