Search code examples
javascriptgoogle-places-autocomplete

How to filter google places autocomplete suggestions?


how to make google autocomplete only suggest places that have zip code and house number? I want to be able to pick only a house with zip code.

What i've tried:

const autocompleteConfig = {
    componentRestrictions: { country: {{country}} },
    types: ['address'],
};

const autocomplete = new google.maps.places.Autocomplete({{element}}, autocompleteConfig);

The problem with the result that i am getting - some suggestions dont have zip code or house number.


Solution

  • google places service should be used.

    const autocomplete = new google.maps.places.Autocomplete(this.searchElementRef.nativeElement);
    const placesService = new google.maps.places.PlacesService(this.searchElementRef.nativeElement);
    

    First get autocomplete predictions:

    placesService.getPlacePredictions({input: 'your place string'})
    

    Then get place zip and house_number of every prediction

    placesService.getDetails({placeId: placeIdFromPrediction})
    

    Then filter suggestions.