Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofill Address + Google Maps API

Tags:

I want to present my users with a text box wherein they can type in their address. As they type their address, I want to provide the users with suggestions/predictions of what address they are trying to type. I'm also concerned about the relevance of this address (e.g. that the address is not an address halfway across the world).

Is this possible? I'm using jQuery.

like image 215
StackOverflowNewbie Avatar asked Oct 30 '10 23:10

StackOverflowNewbie


People also ask

How do I create an autocomplete address field in Google Maps API?

First, enable Google Places API Web Service. Get the API key. You will have to use it in the script tag in html file. Use script file to load the autocomplete class.

Is Google Maps autocomplete API free?

The autocomplete request is available at no charge, and the subsequent Place Details call gets charged based on regular Place Details pricing. A Place Details request generates Data SKUs (Basic, Contact, and/or Atmosphere) – depending on the fields that are specified in the request.


2 Answers

You can use Google Places API to have an autocomplete for address. When address is selected, address_components field contains structured address data (e.g. street, city, country) and could be easily converted into separate fields.

Here is a short working demo:

<!DOCTYPE html> <html>     <head>         <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>     </head>     <body>         <input type="text" id="address" style="width: 500px;"></input>          <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=places&language=en-AU"></script>         <script>             var autocomplete = new google.maps.places.Autocomplete($("#address")[0], {});              google.maps.event.addListener(autocomplete, 'place_changed', function() {                 var place = autocomplete.getPlace();                 console.log(place.address_components);             });         </script>     </body> </html> 
like image 110
Maksym Kozlenko Avatar answered Oct 15 '22 06:10

Maksym Kozlenko


Take a look at address-suggestion, and the demo. It's implemented with jQuery and Google Maps geocode API.

like image 31
user471245 Avatar answered Oct 15 '22 06:10

user471245