Google geocode api没有返回邻居作为回应

时间:2018-02-02 13:02:00

标签: google-maps google-api geolocation google-geocoder google-location-services

我正在使用Google Geocode API从纬度和经度中获取邻域。

以下是请求网址

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请求中传递的纬度和经度的邻域名称。

我已经检查了geocode google API文档,他们在这里显示了邻居,请查看以下链接和回复。

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

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

提前谢谢。

1 个答案:

答案 0 :(得分:1)

所以要回答你的问题,如果你想在响应中获得邻域,你可以遍历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;
}

我希望你明白这个想法