google places api hierarchy - 邻里/郊区 - >城市 - >地区 - >国家

时间:2014-08-21 17:36:09

标签: json hierarchy google-places-api google-places geonames

问题 - 人们根据他们的兴趣在各个地方张贴,而其他人则在更大的区域搜索帖子。 例如 - 用户A在悉尼郊区邦迪海滩下的帖子。 用户B在悉尼搜索帖子。

期望的结果 - 自Bondi位于悉尼境内后,用户B可以看到用户A的帖子。

解决方案:

在google places API中查找,它告诉您郊区X是Y市,依此类推。 在我的例子中 - 我想获得有关邦迪的信息,告诉我它属于哪个城市。

这是我对Bodni Beach的简单查询(我使用auto complete因为它是一个自动完成下拉列表): https://maps.googleapis.com/maps/api/place/autocomplete/json?sensor=true&input=bondi%20beach&key=myKey

{
   "predictions" : [
      {
         "description" : "Bondi Beach, New South Wales, Australia",
         "id" : "ffa6ad484d2fec594378fb2a24e8328a2bc4459f",
         "matched_substrings" : [
            {
               "length" : 11,
               "offset" : 0
            }
         ],
         "place_id" : "ChIJWTi8xgGyEmsRgK0yFmh9AQU",
         "reference" : "CkQ_AAAAMnfwZeK5SV6uPBnPr34JVdHudAGxMtna--JWHtwe7t81gLBGgsB6mRgmj375m7KReRhpMq1Y6AYxCrC2MnYn8hIQQfASvoG5-7LwRcVR7ELjNxoURh-iBs6M2dmrU8wLKIPcFFgymn4",
         "terms" : [
            {
               "offset" : 0,
               "value" : "Bondi Beach"
            },
            {
               "offset" : 13,
               "value" : "New South Wales"
            },
            {
               "offset" : 30,
               "value" : "Australia"
            }
         ],
         "types" : [ "locality", "political", "geocode" ]
      },
      {
         "description" : "Bondi Beachouse YHA, Fletcher Street, Bondi Beach, New South Wales, Australia",
         "id" : "fdab345a9db5c6cfcddd7cd561a8b83acc538d73",
         "matched_substrings" : [
            {
               "length" : 11,
               "offset" : 0
            }
         ],
         "place_id" : "ChIJzwpdVYStEmsRd6F0Wa1NQ2I",
         "reference" : "CmRbAAAA_AFGY8xaYlQebohzYWKrvNoMiJAktObvU4Vb03ORpmf7-ZTlv3BkU3dJPdAIBMtgol8ngdxC1NhVZA7AsAnkRfe4WPT7GJlqkIa_S0Hsb87Dlt-OT1Dt5uQNg_ei4cRPEhCvI7pwL5qD1US6yrOxvVpXGhRQxfEObV1zUxys2a2EZTkbKUdMbg",
         "terms" : [
            {
               "offset" : 0,
               "value" : "Bondi Beachouse YHA"
            },
            {
               "offset" : 21,
               "value" : "Fletcher Street"
            },
            {
               "offset" : 38,
               "value" : "Bondi Beach"
            },
            {
               "offset" : 51,
               "value" : "New South Wales"
            },
            {
               "offset" : 68,
               "value" : "Australia"
            }
         ],
         "types" : [ "establishment" ]
      },
      {
         "description" : "Bondi Beach Car Park, Campbell Parade, Bondi Beach, New South Wales, Australia",
         "id" : "ba6c34f150f7f3ac035d731662f2a08a22a3b5dd",
         "matched_substrings" : [
            {
               "length" : 11,
               "offset" : 0
            }
         ],
         "place_id" : "ChIJXch67J6tEmsRFXD11Ce64WA",
         "reference" : "CmRcAAAA_H6cxlTUVW2JHFb5EO0MQAL-d1M-_6_DXOQAvw6xJOFSri4HaIvodY6lcZH3F8rrAlyaXqOM4lcgbegqvVEPScyRE20iiJSXC6jHPxH5EwplZGPWx7rxy34l1uweXgmhEhBirrakiRpK-Zd_naBW7Hd8GhTiujZAiOEDsWj9tuP3bumLR6SamQ",
         "terms" : [
            {
               "offset" : 0,
               "value" : "Bondi Beach Car Park"
            },
            {
               "offset" : 22,
               "value" : "Campbell Parade"
            },
            {
               "offset" : 39,
               "value" : "Bondi Beach"
            },
            {
               "offset" : 52,
               "value" : "New South Wales"
            },
            {
               "offset" : 69,
               "value" : "Australia"
            }
         ],
         "types" : [ "establishment" ]
      },
      {
         "description" : "Bondi Beach Public School, Campbell Parade, Bondi Beach, New South Wales, Australia",
         "id" : "e808bb9453705761b5b1672065a5aaa5b1b5b52a",
         "matched_substrings" : [
            {
               "length" : 11,
               "offset" : 0
            }
         ],
         "place_id" : "ChIJaXfeKJ6tEmsRM_pgTjpurLs",
         "reference" : "CnRiAAAAN-CRd33lyhCZBryEwtPxVdxweAC-WcxikMSlmiP83st3Eu0qt5tOdx_Kxvfb6aTH_F3szZETbrRYSXb8-Ql-NSdXFhIeZjIf3au9qrwIII7y9-ZhFRImHCeYRJ1xslrS2dal0_lulOh_dY39jgofrRIQoVco7kZBb4DgSaACK9h4ARoUf98GpsdtuHPVY615rIszSUVqRpM",
         "terms" : [
            {
               "offset" : 0,
               "value" : "Bondi Beach Public School"
            },
            {
               "offset" : 27,
               "value" : "Campbell Parade"
            },
            {
               "offset" : 44,
               "value" : "Bondi Beach"
            },
            {
               "offset" : 57,
               "value" : "New South Wales"
            },
            {
               "offset" : 74,
               "value" : "Australia"
            }
         ],
         "types" : [ "establishment" ]
      },
      {
         "description" : "Bondi Beach View, Campbell Parade, Sydney, New South Wales, Australia",
         "id" : "16758032a0384967c04985914cd861dc2c488fe8",
         "matched_substrings" : [
            {
               "length" : 11,
               "offset" : 0
            }
         ],
         "place_id" : "ChIJr6fcCputEmsRtFcOcREuc48",
         "reference" : "CmRUAAAAK3dQSHexE9SerblRCR84jdJ8ScqqDwteRMH2H1f_uCRVQNVsFmgELlMlWWCG2mMbbNOL0yQRv2K0Bx2aqKIe3Bt236LvDl5sxjDaRvdd7QTdBATMJsTHec-xXc3z9c02EhC-JDoD5ln1Q-qdeb-urugLGhSur_7TThy4ekl9yoXRFzZwZmn26w",
         "terms" : [
            {
               "offset" : 0,
               "value" : "Bondi Beach View"
            },
            {
               "offset" : 18,
               "value" : "Campbell Parade"
            },
            {
               "offset" : 35,
               "value" : "Sydney"
            },
            {
               "offset" : 43,
               "value" : "New South Wales"
            },
            {
               "offset" : 60,
               "value" : "Australia"
            }
         ],
         "types" : [ "establishment" ]
      }
   ],
   "status" : "OK"
}

正如你所看到的,唯一告诉我邦迪是悉尼一部分的结果是“邦迪海滩景观”,它没有意义,也没有帮助解决我的问题。

我在世界各地的许多其他地方遇到过这个问题。

如何获得场所层次结构的准确信息?

2 个答案:

答案 0 :(得分:7)

我有类似的问题。我做了一些研究,但没有找到准确的解决方案。目前我正在使用以下解决方法,这对我的情况来说已经足够了。

首先,获取一个地方的坐标(你可以在自动完成结果中找到place_id):

https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJWTi8xgGyEmsRgK0yFmh9AQU&key=yourKey

之后,使用这些坐标和所需的result_types执行反向地理编码请求:country, administrative_area_level_1, administrative_area_level_2, locality

例如:

https://maps.googleapis.com/maps/api/geocode/json?latlng=-33.8914755,151.2766845&result_type=locality&key=yourKey

这不是100%准确的方法。它为某些位置提供了多个结果(如您所见,Bondi Beach就是其中之一),但您可以尝试通过其他一些属性选择一个(在这种情况下,colloquial_area类型的结果是正确的一个)。

我希望,我的回答至少会对你有所帮助。

答案 1 :(得分:0)

你可以

console.log(places.address_components)

然后使用检查浏览器上的控制台。 它将显示访问所需信息所需的层次结构和键。郊区/社区应显示为 'sublocality_level_1'