15

Problem -
people posting in various places based on their interest while other people are searching for posts in greater areas.

e.g. - user A posts under Bondi Beach, a suburb of Sydney.
user B searches for posts under Sydney.

Desired outcome - user B gets to see user A's post since Bondi is located inside Sydney.

Solution:

Find in Google places API where it tells you that suburb X is of city Y and so forth.

In my example - I would like to get information about Bondi that tells me to which city it belongs.

Here is my simple query for Bondi Beach (I use auto complete because it's an autocomplete dropdown): 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"
}

As you can see, the only result that tells me that Bondi is part of Sydney is the "Bondi Beach View", which doesn't make sense nor does it help solve my problem.

I experience this issue with many other places all over the world.

How can I get accurate info of places hierarchy ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
giLisH
  • 196
  • 1
  • 12

2 Answers2

9

I had similar problem. I did some research and didn't find accurate solution. Currently I'm using the following workaround which is good enough for my case.

First, get coordinates of a place (place_id you can find in autocomplete results):

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

After that execute reverse geocoding requests with these coordinates and desired result_types: country, administrative_area_level_1, administrative_area_level_2, locality.

For example:

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

This is not 100% accurate approach. It gives more than one results for some locations (as you can see, Bondi Beach is one of them), but you can try to choose one by some other attributes (in this case, the result with colloquial_area type is correct one).

I hope, my answer will help you at least a little bit.

Marat Safin
  • 1,793
  • 20
  • 28
  • thanks !! it's been two years since, so not really relevant - but i checked your solution and it does the job :). – giLisH Jan 08 '17 at 12:25
0

You can just

console.log(places.address_components)

then use check the console on the browser. It will show the hierarchy and the keys you need to access the information you want. Suburb/Neighbourhood should show up as 'sublocality_level_1'

NonameLover
  • 90
  • 1
  • 8