2

与此主题相关的两个问题。首先,以样本地址“方桥社区居委会”为例,我先直接在谷歌地图上查了一下,我得到了它的位置和英文翻译“方桥社区居委会”。但是,当我尝试运行代码时

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"

我无法获得其邮政编码信息。当我使用以下代码时

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

我可以得到它的邮政编码信息。

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

为什么有区别?我可以知道这个包是如何工作的或者谷歌地图是如何工作的,我可以通过什么方式确定中国的一个位置的邮政编码?

4

2 回答 2

1

我不完全确定谷歌地图如何存储、排序或返回它的数据。但是,通过您的示例,您可以使用函数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"

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

于 2017-06-19T11:23:34.743 回答
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
于 2017-12-05T03:43:31.397 回答