Google API - Autocomplete does not bring ZIP

Asked

Viewed 1,348 times

2

I’m trying to use Google services to get addressing. The idea is to inform the address or zip code and upload the data street, neighborhood, zip code, city and state.

I was able to obtain most of the data, but the zip code is not being returned in the result. Follow the service call and the result (since I changed the key to remove, it won’t work. But the URI is ok):

https://maps.googleapis.com/maps/api/place/autocomplete/json?key=removei&components=country:br&input=rua+embaixador+Abelardo+Bueno

    {
   "predictions" : [
      {
         "description" : "Avenida Embaixador Abelardo Bueno - Barra da Tijuca, Rio de Janeiro - RJ, Brasil",
         "id" : "31fc7076c2289ecf1b100f0a769553d5341ac002",
         "matched_substrings" : [
            {
               "length" : 33,
               "offset" : 0
            }
         ],
         "place_id" : "ElBBdmVuaWRhIEVtYmFpeGFkb3IgQWJlbGFyZG8gQnVlbm8gLSBCYXJyYSBkYSBUaWp1Y2EsIFJpbyBkZSBKYW5laXJvIC0gUkosIEJyYXNpbA",
         "reference" : "CmRUAAAAkQ98koLpBb_VEBonelaZ8iCxYEHtJp0sDeH4gtBzgEnkOs4hpwI7Jaxd0Nq7ZAo1pfsWy9YRLWFkIz0D4EyAZC6ncoK-rQ-dTr9lMWVeFqhZTK50x-zweb3LFcb_zAJDEhD7GsVJdIBYX0_ldipY-272GhSqcdx6p9Ml9BgchUoHct1wglHwqQ",
         "structured_formatting" : {
            "main_text" : "Avenida Embaixador Abelardo Bueno",
            "main_text_matched_substrings" : [
               {
                  "length" : 33,
                  "offset" : 0
               }
            ],
            "secondary_text" : "Barra da Tijuca, Rio de Janeiro - RJ, Brasil"
         },
         "terms" : [
            {
               "offset" : 0,
               "value" : "Avenida Embaixador Abelardo Bueno"
            },
            {
               "offset" : 36,
               "value" : "Barra da Tijuca"
            },
            {
               "offset" : 53,
               "value" : "Rio de Janeiro"
            },
            {
               "offset" : 70,
               "value" : "RJ"
            },
            {
               "offset" : 74,
               "value" : "Brasil"
            }
         ],
         "types" : [ "route", "geocode" ]
      }
   ],
   "status" : "OK"

Does the zip code not return itself? Is there any parameter that determines that the zip code needs to be included?

  • As far as I searched, if you do not inform the street number, it will not return zip code, because the same street can have several zip codes

  • Felipe, it makes perfect sense. But testing the address "Rua Irutim, 86 - Penha Circular, Rio de Janeiro - RJ, Brasil" on the link https://jsfiddle.net/api/post/library/pure/ provided by Google does not return the zip code. Thanks for the return.

  • How can I enter the address number in the URI?

1 answer

2


The problem with this approach is that Google location data does not always have the zip code, as you may have noticed.

Therefore, to work with Ceps, I suggest the use of Web Services like the Viacep that has a gigantic database with the Ceps of Brazil.

Unfortunately, you will have to get the information, update the fields and do validations manually, however the operation of Web Service is quite simple...

Just access the URL with the address and you will have the zip code as return.

https://viacep.com.br/ws/RS/Porto%20Alegre/Domingos+Jose+Poli/json/

[
  {
    "cep": "91790-072",
    "logradouro": "Rua Domingos José Poli",
    "complemento": "",
    "bairro": "Restinga",
    "localidade": "Porto Alegre",
    "uf": "RS",
    "unidade": "",
    "ibge": "4314902",
    "gia": ""
  }
]       

It also works the other way around: you inform the zip code and the service returns address details.

https://viacep.com.br/ws/01001000/json/

{
  "cep": "01001-000",
  "logradouro": "Praça da Sé",
  "complemento": "lado ímpar",
  "bairro": "Sé",
  "localidade": "São Paulo",
  "uf": "SP",
  "unidade": "",
  "ibge": "3550308",
  "gia": "1004"
}

For cases where a street may have more than one zip code, Viacep will return an array with all available zip codes from that street:

https://viacep.com.br/ws/SP/Sao%20Paulo/Avenida%20Paulista/json/

[
  {
    "cep": "01311-909",
    "logradouro": "Avenida Paulista",
    "complemento": "491",
    "bairro": "Bela Vista",
    "localidade": "São Paulo",
    "uf": "SP",
    "unidade": "",
    "ibge": "3550308",
    "gia": "1004"
  },
  {
    "cep": "01311-905",
    "logradouro": "Avenida Paulista",
    "complemento": "347",
    "bairro": "Bela Vista",
    "localidade": "São Paulo",
    "uf": "SP",
    "unidade": "",
    "ibge": "3550308",
    "gia": "1004"
  },
  {
    "cep": "01310-000",
    "logradouro": "Avenida Paulista",
    "complemento": "até 610 - lado par",
    "bairro": "Bela Vista",
    "localidade": "São Paulo",
    "uf": "SP",
    "unidade": "",
    "ibge": "3550308",
    "gia": "1004"
  },
  {
    "cep": "01310-933",
    "logradouro": "Avenida Paulista",
    "complemento": "2444",
    "bairro": "Bela Vista",
    "localidade": "São Paulo",
    "uf": "SP",
    "unidade": "",
    "ibge": "3550308",
    "gia": "1004"
  }
...
  • Perfect amelco. It seems to me that Google does not have the updated ceps. Thinking about Felipe’s placement, we can have more than one zip code for the same street, avenue... How could I pass the address number to Viacep? I am looking on the site and on Google, but I did not succeed. Thank you so much for your solution.

  • I believe Viacep won’t accept numbers in the consultation, so it won’t be as accurate as expected. But referring to the name of the street, city and state, the Viacep returns a Vector with the possible results. That is, if the street has 2 zip codes, it should list both. It is up to you to treat this for the user.

  • I added to the answer this possibility of multiple Zip Codes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.