Saturday, January 19, 2013

Google Address AutoComplete Tutorial

This is very basic for google address lookup/auto completion tutorial.
First thing you need to do is include google maps javascript to your page.

<script src=""></script>

Next step is to initialize Autocomplete. When you initialize Autocomplete, it basically starts a listener which listens for an input. Following code does that.

var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input);

When you start typeing it looks like this.

When user selects an address, value of input field get updated to selected address. You do not need to have nay extra code. However in case if you want to read selected address, you will need to add "place_changed" google maps event listener. It returns PlaceResult object. Following code shows how you can get PlaceResult object.

google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();

You can find a demo here: Google Address Auto Complete Demo In this example, I am loading address lookup form using an ajax.

You can download demo source from github: View source code

Here is the complete snippet.


  1. thanks a lot. I was looking for this indeed

  2. Hey, it doesent work for me. I put the first four Lines into my Code, but when i type in the SearchField no Autocompletion appears.

    do you have an advice?

  3. what if i want to add a new field into the autocomplete result?

  4. Not working for me either.

    I can tell that the browser is asking google for the results, they are just not being displayed.

  5. Which browser are you trying with? I just tested in chrome And it is working.

  6. How to get Postal Code at end of each address?

  7. Hey, this isnt working for me. If I place the exact code provided by you, I receive the following error:
    SyntaxError: missing : after property id
    $(function() {
    Can you please help?

  8. Excelente Trabajo lo voy a implementar en:

  9. Podrian ayudarme en esto,

    he tratado de implementar que está en este post pero no logro dar solución.

    se los agradecere demasiado.!