首页 > 解决方案 > 谷歌地图 API JSON 响应各不相同

问题描述

当用户在输入框中键入时,我正在使用 Google 的“地点”API 来检索城镇/城市名称。我看到一些非常奇怪的行为,有时该地点的 id 将存储在 key 下,place_id而其他时候则存储为placeId. 下面是一个从 API 返回的示例对象以获得更多上下文:

 "predictions" : [
      {
         "description" : "Sydney NSW, Australia",
         "matched_substrings" : [
            {
               "length" : 1,
               "offset" : 0
            }
         ],
         "place_id" : "ChIJP3Sa8ziYEmsRUKgyFmh9AQM",
         "reference" : "ChIJP3Sa8ziYEmsRUKgyFmh9AQM",
         "structured_formatting" : {
            "main_text" : "Sydney",
            "main_text_matched_substrings" : [
               {
                  "length" : 1,
                  "offset" : 0
               }
            ],
            "secondary_text" : "NSW, Australia"
         },
         "terms" : [
            {
               "offset" : 0,
               "value" : "Sydney"
            },
            {
               "offset" : 7,
               "value" : "NSW"
            },
            {
               "offset" : 12,
               "value" : "Australia"
            }
         ],
         "types" : [ "colloquial_area", "locality", "political", "geocode" ]
      },
    ]

正如您在这种情况下所看到的,地点的 ID 存储在 key 下place_id,如上所述,即使我查询完全相同的地点,有时也会随机更改。

有没有其他人遇到过这个?

标签: javascriptgoogle-maps

解决方案


推荐阅读