Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Places API Web Service (PHP): how to get 'administrative_area_2 place_id' for a given 'place_id'

My webapp receives a place_id for a user-selected geo. For example: if the user chooses on the frontend a place called "Comacchio" (it's a town, part of the administrative_area_level_2 called Province of Ferrara) my webapp recives the place_id for Comacchio: ChIJ0yHebbB1fkcR7YujotES0dA.

When I query maps.googleapis.com for said place_id, I get a whole lot of information (see below) but not the one I need: what I need is the place_id for the administrative_area_level_2 of the input place_id (which would be ChIJ7y4rEyxCfkcRwH-6_AubBwM for Provincia di Ferrara).

As you can see below, Provincia di Ferrara is returned as a string, but not as the place_id I need.

Array (
    [html_attributions] => Array(
    )

    [result] => Array (
        [address_components] => Array (
            [0] => Array (
                [long_name] => Comacchio
                [short_name] => Comacchio
                [types] => Array (
                    [0] => locality
                    [1] => political
                )
            )

            [1] => Array (
                [long_name] => Comacchio
                [short_name] => Comacchio
                [types] => Array (
                    [0] => administrative_area_level_3
                    [1] => political
                )
            )

            [2] => Array (
                [long_name] => Provincia di Ferrara
                [short_name] => FE
                [types] => Array (
                    [0] => administrative_area_level_2
                    [1] => political
                )
            )

            [3] => Array (
                [long_name] => Emilia-Romagna
                [short_name] => Emilia-Romagna
                [types] => Array (
                    [0] => administrative_area_level_1
                    [1] => political
                )
            )

            [4] => Array (
                [long_name] => Italy
                [short_name] => IT
                [types] => Array (
                    [0] => country
                    [1] => political
                )
            )

            [5] => Array (
                [long_name] => 44022
                [short_name] => 44022
                [types] => Array (
                    [0] => postal_code
                )
            )
        )

        [adr_address] => <span class="postal-code">I-44022</span> <span class="locality">Comacchio</span> <span class="region">FE</span>, <span class="country-name">Italy</span>
        [formatted_address] => 44022 Comacchio FE, Italy
        [geometry] => Array (
            [location] => Array (
                [lat] => 44.6940053
                [lng] => 12.1793532
            )

            [viewport] => Array (
                [northeast] => Array (
                    [lat] => 44.7056942
                    [lng] => 12.2000628
                )

                [southwest] => Array (
                    [lat] => 44.6839703
                    [lng] => 12.15889
                )
            )
        )

        [icon] => http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png
        [id] => cb8042e2b729756e683d2aff008871f9876081dd
        [name] => Comacchio
        [place_id] => ChIJ0yHebbB1fkcR7YujotES0dA
        [reference] => CoQBdgAAAGSwypFqE8NNsCqFFroul7U-c8ACswn9Zd_O89SAK6nxPTpsevzOLaa9rQa0-ms2SXDgnrAxKkQ7av8KHxjRHL0UB_GD_xBzMM2u5iYCDVhoLtwSKMifSJAX7T0q9pdqeB6kzPa3IbLGpdDcaMOOAWZ8gf1gtbNjr9Wf3oo5cV4JEhAWQYrsRynoan3_W0DMElMSGhQb2BUPnW0s_cCUgeHkskTZwSPNeg
        [scope] => GOOGLE
        [types] => Array (
            [0] => locality
            [1] => political
        )

        [url] => https://maps.google.com/maps/place?q=44022+Comacchio+FE,+Italy&ftid=0x477e75b06dde21d3:0xd0d112d1a2a38bed
        [vicinity] => Comacchio
    )

    [status] => OK
)
like image 346
Dr. Gianluigi Zane Zanettini Avatar asked Jul 23 '15 21:07

Dr. Gianluigi Zane Zanettini


1 Answers

You can try to call Google Places API Web Service from PHP with administrative_area_level_2 (add a country to a query param, to get more accurate predictions) value of selected place, like:

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Provincia%20di%20Ferrara,%20Italy&types=(regions)&language=pt_BR&key={private_key_removed}

The output should be:

{
   "predictions" : [
      {
         "description" : "Provincia di Ferrara, Itália",
         "id" : "b5fa0282b59be22bd0ad64f97011f744db1aeed8",
         "matched_substrings" : [
            {
               "length" : 20,
               "offset" : 0
            }
         ],
         "place_id" : "ChIJ7y4rEyxCfkcRwH-6_AubBwM",
         "reference" : "CkQ1AAAAqCcMPeJiVWhf2mlBLCJVf82WAxOUARNWqT6NWbp7AEb2nlub1ax3iUMDTKsCAvM_Y6Uylc78mSB_QB5bECVkGxIQxCwIgZ4_WC6fAPJB_N7o0RoU8m_Sdb6CpSyaZeW2lpi9YoJ8bO0",
         "terms" : [
            {
               "offset" : 0,
               "value" : "Provincia di Ferrara"
            },
            {
               "offset" : 22,
               "value" : "Itália"
            }
         ],
         "types" : [ "administrative_area_level_2", "political", "geocode" ]
      }
   ],
   "status" : "OK"
}
like image 68
Alexander Schwarzman Avatar answered Nov 05 '22 18:11

Alexander Schwarzman