【问题标题】:Google Places API Web Service (PHP): how to get 'administrative_area_2 place_id' for a given 'place_id'Google Places API Web Service (PHP):如何获取给定“place_id”的“administrative_area_2 place_id”
【发布时间】:2015-07-23 21:31:14
【问题描述】:

我的 web 应用收到用户选择的地理位置的 place_id。例如:如果用户在前端选择 place called "Comacchio"(它是一个城镇,属于 administrative_area_level_2 的一部分,称为 Province of Ferrara),我的 web 应用程序会接收到 Comacchio 的 place_id:ChIJ0yHebbB1fkcR7YujotES0dA

当我在 maps.googleapis.com 中查询所述 place_id 时,我得到了很多信息(见下文),但不是我需要的:我需要的是管理区域级别 2 的 place_id输入的 place_id(对于 Provincia di Ferrara 将是 ChIJ7y4rEyxCfkcRwH-6_AubBwM)。

正如您在下面看到的,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
)

【问题讨论】:

  • 来吧,伙计,为什么要投反对票?!?
  • 取 lat/lon 并在附近搜索 long_name?

标签: php google-maps google-places-api


【解决方案1】:

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

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"
}

【讨论】:

  • 这就是我现在正在做的事情,减去“将国家/地区添加到查询参数”并按地区过滤。这仍然不是最理想的,因为我们是从非结构化数据字符串中搜索,而不是查询唯一的 place_id。尽管如此,您的两个技巧仍然大大改善了结果集。我担心没有办法像我一直在寻找的那样去做,而你的建议是我们能去的最接近的。已标记为已接受,非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-14
  • 1970-01-01
  • 1970-01-01
  • 2015-02-02
  • 2014-08-26
  • 1970-01-01
相关资源
最近更新 更多