KaisarAhmmed
KaisarAhmmed

Reputation: 49

Get postal code with autocomplete service

I want to show postal code with address. I get address here but i can't find postal code to create custom auto address drop down.

var service = new google.maps.places.AutocompleteService();
var searchStr = '';
$("#search").keyup(function() {
    var searchStr = $("#search").val();

    service.getPlacePredictions({ input: searchStr, types: ['geocode'],
                        componentRestrictions:{ country: 'us' } },
    function (predictions, status) {

        if (status == google.maps.places.PlacesServiceStatus.OK) {
            var results = document.getElementById('results');
            jQuery("#results").empty();
            for (var i = 0, prediction; prediction = predictions[i]; i++) {
                jQuery("#results").append('<li>' + prediction.terms[0].value +','+ prediction.terms[1].value + '</li>');

                    console.log(prediction);

            }
        }
    });

});

I can't get postal code from those code.

Upvotes: 3

Views: 2005

Answers (1)

evan
evan

Reputation: 5691

The getPlacePredictions method returns an array of prediction objects each of which expose a placeId. From Google's documentation:

To retrieve information about the place, pass this identifier in the placeId field of a Place Details request.

You can then get the postal_code from the address_components array returned in the results of the Places Details call.

Hope this helps!

Upvotes: 3

Related Questions