如何识别Google地方详细信息结果是否为州(administrative_area_level_1)

时间:2017-05-10 10:25:05

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

目前我们正在使用Google置贴api的address_components数组来识别搜索到的地点是州还是国家。对于州和国家/地区,我们在搜索提交时传递州代码和国家/地区代码。以下是我们用于识别地点详细结果类型的方案:

州情景: address_components有2个项目,第1个项目为administrative_area_level_1类型,第2个项目为国家/地区类型。

  "address_components" : [
     {
        "long_name" : "California",
        "short_name" : "CA",
        "types" : [ "administrative_area_level_1", "political" ]
     },
     {
        "long_name" : "United States",
        "short_name" : "US",
        "types" : [ "country", "political" ]
     }
  ],

国家/地区场景:address_components有1个项目,其类型有国家/地区

  "address_components" : [
     {
        "long_name" : "United States",
        "short_name" : "US",
        "types" : [ "country", "political" ]
     }
  ],

如果address_components项目列表包含2个以上的项目,则所选地点不会被视为州或国家/地区。虽然很少有像“Oaxaca”或“New York”这样的搜索在address_components数组中返回两个以上的项目。

瓦哈卡:

  "address_components" : [
     {
        "long_name" : "Oaxaca",
        "short_name" : "Oaxaca",
        "types" : [ "locality", "political" ]
     },
     {
        "long_name" : "Oaxaca",
        "short_name" : "Oaxaca",
        "types" : [ "administrative_area_level_2", "political" ]
     },
     {
        "long_name" : "Oaxaca",
        "short_name" : "Oax.",
        "types" : [ "administrative_area_level_1", "political" ]
     },
     {
        "long_name" : "Mexico",
        "short_name" : "MX",
        "types" : [ "country", "political" ]
     }
  ]

纽约

  "address_components" : [
     {
        "long_name" : "New York",
        "short_name" : "New York",
        "types" : [ "locality", "political" ]
     },
     {
        "long_name" : "New York",
        "short_name" : "NY",
        "types" : [ "administrative_area_level_1", "political" ]
     },
     {
        "long_name" : "United States",
        "short_name" : "US",
        "types" : [ "country", "political" ]
     }
  ]

有没有其他方法可以确定搜索地点的详细结果(如瓦哈卡) 是一个州(administrative_area_level_1)

1 个答案:

答案 0 :(得分:0)

当然,您可以使用与address_components字段一起返回的types字段

https://developers.google.com/places/web-service/details#PlaceDetailsResults

  

types []包含描述给定结果的要素类型数组。

如果是国家/地区,

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

你会看到像

这样的东西
{
  "html_attributions":[

  ],
  "result":{
      "address_components":[
      {
        "long_name":"United States",
        "short_name":"US",
        "types":[
          "country","political"
        ]
      }
      ],
      ... 
      "types":[
         "country","political"
      ],
      ...
  },
  "status":"OK"
} 

如果状态如

{
    "html_attributions":[

    ],
    "result":{
        "address_components":[
        {
            "long_name":"Nebraska",
            "short_name":"NE",
            "types":[
               "administrative_area_level_1","political"
            ]
         },
         {
            "long_name":"United States",
            "short_name":"US",
            "types":[
               "country","political"
             ]
         }
         ],
         ...
         "types":[
             "administrative_area_level_1","political","point_of_interest","establishment"
          ],
          ...
      },
      "status":"OK"
}

城市将有类似

的东西
{
    "html_attributions":[

    ],
    "result":{
        "address_components":[
        {
            "long_name":"Oaxaca",
            "short_name":"Oaxaca",
            "types":[
               "locality","political"
            ]
         },
         {
            "long_name":"Oaxaca",
            "short_name":"Oaxaca",
            "types":[
                "administrative_area_level_2","political"
             ]
          },
          {
              "long_name":"Oaxaca",
              "short_name":"Oax.",
              "types":[
                 "administrative_area_level_1","political"
              ]
           },
           {
              "long_name":"Mexico",
              "short_name":"MX",
              "types":[
                 "country","political"
              ]
            }
            ],
            ...
            "types":[
                "locality","political"
            ],
            ... 
        },
        "status":"OK"
}

我希望这有帮助!