Google 地理编码 api 未返回邻域作为响应

Google geocode api not returning neighborhood in response

我正在使用 Google 地理编码 API 从纬度和经度获取邻域。

下面是请求URL-

https://maps.googleapis.com/maps/api/geocode/json?latlng=-15.535573,-47.336828&sensor=true

下面是回复-

{
    "results": [
        {
            "address_components": [
                {
                    "long_name": "1224-1282",
                    "short_name": "1224-1282",
                    "types": [
                        "street_number"
                    ]
                },
                {
                    "long_name": "Rua Visconde de Porto Seguro",
                    "short_name": "R. Visc. de Porto Seguro",
                    "types": [
                        "route"
                    ]
                },
                {
                    "long_name": "Centro",
                    "short_name": "Centro",
                    "types": [
                        "political",
                        "sublocality",
                        "sublocality_level_1"
                    ]
                },
                {
                    "long_name": "Formosa, Goiás",
                    "short_name": "Formosa, Goiás",
                    "types": [
                        "locality",
                        "political"
                    ]
                },
                {
                    "long_name": "Formosa",
                    "short_name": "Formosa",
                    "types": [
                        "administrative_area_level_2",
                        "political"
                    ]
                },
                {
                    "long_name": "Goiás",
                    "short_name": "GO",
                    "types": [
                        "administrative_area_level_1",
                        "political"
                    ]
                },
                {
                    "long_name": "Brazil",
                    "short_name": "BR",
                    "types": [
                        "country",
                        "political"
                    ]
                },
                {
                    "long_name": "73801-670",
                    "short_name": "73801-670",
                    "types": [
                        "postal_code"
                    ]
                }
            ]
}

我的问题:我需要我在 API 请求中传递的经纬度的社区名称。

我已经检查了地理编码 google API 此处的文档他们正在显示附近的响应,请检查下面的 link 和响应。

https://developers.google.com/maps/documentation/geocoding/start

https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "277",
               "short_name" : "277",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Bedford Avenue",
               "short_name" : "Bedford Ave",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Williamsburg",
               "short_name" : "Williamsburg",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Brooklyn",
               "short_name" : "Brooklyn",
               "types" : [ "sublocality", "political" ]
            },
            {
               "long_name" : "Kings",
               "short_name" : "Kings",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "NY",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "11211",
               "short_name" : "11211",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "277 Bedford Avenue, Brooklyn, NY 11211, USA",
         "geometry" : {
            "location" : {
               "lat" : 40.714232,
               "lng" : -73.9612889
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.7155809802915,
                  "lng" : -73.9599399197085
               },
               "southwest" : {
                  "lat" : 40.7128830197085,
                  "lng" : -73.96263788029151
               }
            }
         },
         "place_id" : "ChIJd8BlQ2BZwokRAFUEcm_qrcA",
         "types" : [ "street_address" ]
      },

   ... Additional results truncated in this example[] ...

   ],
   "status" : "OK"
}

如果有人做过这件事,请分享建议。

提前致谢。

因此,为了回答您的问题,如果您想在响应中获取邻域,您可以遍历 component_address 并使用 If 语句检查类型。

var ac = results.address_components;
 // FOR loop to iterate 
if( types == "sublocality_level_1") { // for brasil (and remember that types is also and array)
var neighborhood = ac[i].long_name;
}

我希望你明白了