get zipcode from google places autocomplete

Solutions on MaxInterview for get zipcode from google places autocomplete by the best coders in the world

showing results for - "get zipcode from google places autocomplete"
Rafael
14 Jan 2018
1function initialize() {
2  var input = document.getElementById('id_address');
3  var options = {
4    types: ['address'],
5    componentRestrictions: {
6      country: 'us'
7    }
8  };
9  autocomplete = new google.maps.places.Autocomplete(input, options);
10  google.maps.event.addListener(autocomplete, 'place_changed', function() {
11    var place = autocomplete.getPlace();
12    for (var i = 0; i < place.address_components.length; i++) {
13      for (var j = 0; j < place.address_components[i].types.length; j++) {
14        if (place.address_components[i].types[j] == "postal_code") {
15          document.getElementById('postal_code').innerHTML = place.address_components[i].long_name;
16
17        }
18      }
19    }
20  })
21}
22google.maps.event.addDomListener(window, "load", initialize);