如何始终从R中的ggmap / googleway包中获取zipcode记录?

时间:2017-06-16 06:17:20

标签: r ggmap googleway

与此主题相关的两个问题。首先,通过样本地址'方桥社区居委会,我首先直接从谷歌地图上查看,我得到了它的位置和英文翻译和方桥社区居民'委员会&#39 ;. 但是,当我尝试运行代码时

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

我从上面的中文地址得到的结果是

$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"

我无法获取其postal_code信息。当我使用以下代码时

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

我可以获得它的postal_code信息。

$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"

为什么会有区别? 我可以知道这个包的工作原理或Google Map的工作原理,以及我可以通过哪种方式获取中国某地的邮政编码?

2 个答案:

答案 0 :(得分:1)

我不完全确定Google地图如何存储,排序或返回其数据。 但是,通过您的示例,您可以使用place_id函数中的google_place_details()来获取有关从查询返回的位置的更多详细信息。

因此,您可以执行第一个查询返回的place_id

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)

在结果中,您会看到字段res_details$result$name “方桥社区居民委员会”

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"

这是正确的结果吗?也许不止一个方桥社区居民委员会?

答案 1 :(得分:0)

您可以考虑使用geocode()包中的ggmap功能。

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

获取邮政编码/邮政编码:

> result$postal_code
[1] 230000