我的Web应用程序收到用户选择的地理位置的place_id。例如:如果用户在前端选择了place called "Comacchio"(这是一个城镇,一部分为Administrative_area_level_2的一部分,称为Province of Ferrara),则我的网络应用会获取Comacchio的place_id:ChIJ0yHebbB1fkcR7YujotES0dA

当我在maps.googleapis.com中查询所述place_id时,我得到了很多信息(请参阅下文),但不是我需要的信息:我需要的是输入place_id(即ChIJ7y4rEyxCfkcRwH-6_AubBwM)的Administrative_area_level_2 的place_id。 (Provincia di Ferrara)。

如下所示,Provincia di Ferrara以字符串形式返回,但不是我需要的place_id。

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
)

最佳答案

您可以尝试通过PHP使用选定位置的Administrative_area_level_2(向查询参数添加国家/地区,以获得更准确的预测)值从PHP调用Google Places API Web服务,例如:

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

输出应为:
{
   "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"
}

10-04 16:17