0

I am using Google Geocode API to fetch neighborhood from the latitude and longitude .

Below is the request URL -

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

Below is the response -

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

My Problem : I need the neighborhood name of the the latitude and longitude which I passed in the API request .

I have checked geocode google API Document here they are showing neighborhood in response please check below link and response .

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

Please share suggestion , if anyone done this thing .

Thank you in advance .

Rahul Gaikwad
  • 563
  • 4
  • 9
  • 26
  • 2
    all this components types are strictly based on each country system, have you tried with other addresses in Belgium? Can you find any type "neighborhood" ? Can you tell me the name of the neighborhood that you are aiming for – betofarina Feb 02 '18 at 13:32
  • I need neighborhood name for brazil addresses ( latitude and longitude ) , I have checked in USA addresses ( latitude and longitude) in that the API giving the neighborhood name in results, but not for brazil. So is there and documentation where I will find this country wise response for this API ? – Rahul Gaikwad Feb 05 '18 at 05:16
  • omt, Can you tell me the name of the neighborhood that you are aiming for? give me a real address or coordinates that are in this neighborhood – betofarina Feb 05 '18 at 10:32
  • I am not targeting any single neighborhood . My requirement is very simple I need neighborhood name from the users current location(lat long) , means I want to pass lat long in request and in response i need the neighborhood name of that geo location . Here is the example of neighborhood name "Centro" I have passed its lat long in the request. https://maps.googleapis.com/maps/api/geocode/json?latlng=-15.535573,-47.336828&sensor=true This is for brazil neighborhoods . – Rahul Gaikwad Feb 05 '18 at 12:57
  • finally! ok now... centro is a neighborhood and it is under the address_components type: sublocality and sublocality_level_1 , now I want you to do several requests in neighborhoods that you know (in brasil), and check if they all have one of this 2 types (sublocality and sublocality_level_1) under their component address (where the neighborhood name display). I would do it but don't know brasil that much – betofarina Feb 05 '18 at 13:30
  • I have checked with some of neighborhoods of brazil and its coming in sublocality_level_1 , But my problem is that do i consider this as a neighborhood ? because in google api docs sub-locality and neighborhood both are different address types See below doc. https://developers.google.com/maps/documentation/geocoding/intro#Types – Rahul Gaikwad Feb 05 '18 at 14:02
  • yes! thats it, as I said in my 1st comment this types can be different from country to country, what is a neighboorhood in USA may be something different in brasil, now you know for sure that is in sublocality_level_1. if you need something else let me know – betofarina Feb 05 '18 at 14:11

1 Answers1

1

So to answer you question if you want to get the neighborhood in the response you can iterate through the component_address and with a If statment check the types.

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

I hope you get the idea

betofarina
  • 1,066
  • 7
  • 9
  • Yes I totally understand your answer and appreciate, I have checked with multiple addresses of brazil and neighboorhood is coming in sublocality_level_1 in response , But my question is , Is there is in written statement in google API doc , that we can consider sublocality_level_1 as neighboorhood for brazil ? – Rahul Gaikwad Feb 06 '18 at 05:39
  • I am afraid they don't and they won't anytime in the near future as the APIs are constantly changing, try to imagine to keep updated a doc about all the countries. My suggestion is that try to find a pattern in the component_address or if you feel lucky request that they add this info to the documentation in the issuetracker https://issuetracker.google.com – betofarina Feb 06 '18 at 08:14