Autocomplete Address Form Using Google Map API
Google map is one of the most widely used APIs of Google as a majority of websites use Google maps for showing address location. For a static address it’s pretty simple. All you need to do is input the address and the map will show the nearest location. The problem arrives when the address is dynamically putted by the user. Suppose for an event in Event Organizer Server, one enters the location. The main component used while taking input location is Google Autocomplete. But we went a step further and parsed the entire address based on city, state, country, etc. and allowed the user to input the details as well, which gave them the nearest location marked in Map if Autocomplete couldn’t find the address.
Autocomplete Location
As we can see, in the above input box we get suggestions from Google Map upon entering the first few letters of our address. To do this, we need the API. You can find an example code of how to do this here.
After this is done, what we want is to not only include this address but provide a form to fill up the entire address in case some parts were missing. The function that the autocomplete listens to is _“place\_changed”_
. So once we click on one of the options, this event is triggered. Once the event is triggered, we use the _autocomplete.getPlace()_
to get the complete address JSON. The JSON looks something like this:
Now, what we do is we create a form with input fields having the same ID as the ones we require(e.g., country, administrative_area_level_1,locality, etc.). After that, we select the ‘long_name or the short_’name from this JSON and put the value in the corresponding input field with the IDs. The code for the process after getting the JSON is elaborated here.
Editing Address Form
After doing this it looks something like this:
However, now the important part is to show the map according to these fields. Also, every time we update a field, the map should be updated. For this we use a hack. Instead of removing the initial location field completely, we hide the field but keep the binding to Autocomplete intact. As a result, the map is shown when we select a particular address.
Now, when we update the fields in the address form, we append the value of this field to the value in the initial location field. Although the field is hidden, it is still bound to Autocomplete. As a result, as soon as we append something to the string contained in the field, the map gets updated. Also, the updated value gets stored to the DB. Thus, with every update in the field, the pointer is moved to the nearest location formed by appending all the data from the form.
After saving the location data to DB, if we wish to edit it, we can get back the JSON by making the same request with the location value. This will return the same address form and the map accordingly.
Finally, it looks something like this:
If you would like to learn more about Google Maps API here are some related articles:
Drawing a Google Map With Data from an API
Custom Infobox Label with Google Maps
This tutorial is originally posted by the author on fossasia. This version has been edited for clarity and may appear different from the original post.
Hello Saptak. Congratulations for your API! I can try to develop a similar api too. I “have mixed” the code of two apis of Google:
1.https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete#top_of_page
2.https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform
But i have “result” one at a time, and not at both of them!
Have you any idea of the pottential reason? Maybe I have to use 2 different keys?
Thank you in advance.
Hello Saptak, I also require a similar scenario for an asp.net/C3 app I am writing, but am struggling to combine to two examples. I am sure some of my issue lies within the javascript. Is there anyway you could post a working example of what you have outlined above?
Any help you may able to lend would be very appreciated.
hello what I need to do is this, could you tell me how to do it? this is the link http://vecinosconectados.cl/loespejo/#/new-report my mail is maximilianolerner@gmail.com thank you very much