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="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></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();
alert(place.formatted_address);

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.

27 comments:

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

    ReplyDelete
  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?

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

    ReplyDelete
  4. Not working for me either.

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

    ReplyDelete
  5. Which browser are you trying with? I just tested in chrome http://priyankgandhi.com/googleapi/ And it is working.

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

    ReplyDelete
  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?

    ReplyDelete
  8. Excelente Trabajo lo voy a implementar en:
    http://calimaframework.com

    ReplyDelete
  9. Podrian ayudarme en esto, http://es.stackoverflow.com/questions/8637/c%C3%B3mo-realizar-autocompletar-en-mi-cuadro-de-b%C3%BAsqueda


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

    se los agradecere demasiado.!

    ReplyDelete
  10. Develop strong programming skills using Java to create reliable and platform-independent applications. java training online

    ReplyDelete
  11. Learn the fundamentals of creating intuitive, engaging, and user-centered digital designs.ui ux online course

    ReplyDelete
  12. Gain skills to design data structures that ensure consistency, accuracy, and efficient data management. data modeling courses online

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. Learn how to access and manage digital land records through the Bhoomi land information system. dell boomi full course

    ReplyDelete
  15. Python training offers in-depth understanding of Python fundamentals and applications. It explains variables, loops, and functions clearly. This python training improves hands-on programming skills. Students build real-world programs. Modules, packages, and standard libraries are included. Web and automation basics are covered. Problem-solving techniques are explained. It prepares industry-ready Python professionals.

    ReplyDelete
  16. Enrolling in a servicenow admin course is a smart step for anyone looking to enter the IT service management field. It helps you understand core concepts, platform navigation, and real-time applications with ease.servicenow admin course

    ReplyDelete
  17. This blog clearly explains the value of MuleSoft certification for career growth and professional credibility.mule soft certification

    ReplyDelete
  18. A mulesoft developer course helps in building strong skills in API design and implementation.
    mulesoft developer course

    ReplyDelete
  19. This article makes it easier to understand what to expect from ServiceNow classes. Very helpful for beginners.servicenow classes

    ReplyDelete
  20. Very informative! data modeling course
    plays a crucial role in organizing and managing data. Skill-based training helps learners gain confidence in designing accurate and efficient database systems.

    ReplyDelete
  21. A great explanation of servicenow administrator course, covering both basics and career opportunities.servicenow administrator course

    ReplyDelete
  22. The ServiceNow classes mentioned here seem well-organized and practical for gaining real-time experience.servicenow classes

    ReplyDelete
  23. ServiceNow classes with practical exposure are very important. This blog explains it clearly.servicenow classes

    ReplyDelete
  24. ServiceNow Classes provide step-by-step guidance on platform navigation, system configuration, and IT workflow management. It is suitable for both beginners and working professionals.servicenow classes

    ReplyDelete
  25. Excellent article with valuable information. I learned several new things from this post today. Looking forward to more content from you.online business analyst classes

    ReplyDelete
  26. A full stack python developer course helps learners understand frontend and backend web development using Python technologies. It explains databases, frameworks, APIs, and application architecture clearly. This full stack python developer course helps students gain practical experience through real-time projects. Learners develop complete web applications from scratch. The course prepares learners for full stack development careers.

    ReplyDelete