15

问题 -
人们根据自己的兴趣在各个地方发帖,而其他人则在更大范围内搜索帖子。

例如 - 用户 A 在悉尼郊区的邦迪海滩下发帖。
用户 B 在 Sydney 下搜索帖子。

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

解决方案:

在 Google Places API 中查找它告诉您郊区 X 属于城市 Y 等等。

在我的示例中 - 我想获取有关邦迪的信息,告诉我它属于哪个城市。

这是我对邦迪海滩的简单查询(我使用自动完成,因为它是一个自动完成下拉菜单): 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"
}

如您所见,唯一告诉我邦迪是悉尼一部分的结果是“邦迪海滩景观”,这没有意义,也无助于解决我的问题。

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

如何获得地点层次结构的准确信息?

4

2 回答 2

8

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

首先,获取一个地点的坐标(您可以在自动完成结果中找到 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% 准确的方法。它为某些位置提供了多个结果(如您所见,邦迪海滩就是其中之一),但是您可以尝试通过其他一些属性来选择一个(在这种情况下,带有colloquial_area类型的结果是正确的)。

我希望,我的回答至少对你有一点帮助。

于 2016-12-22T20:23:14.557 回答
0

你可以

console.log(places.address_components)

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

于 2020-07-13T14:44:31.720 回答