0

I tried to find a solution only for this issue without success. So i will appreciate your help.

I would like to get autocomplete address from google api. I'm using this code now:

var input, options, zip;
$( document ).ready(function() {
    options = {
        types: ['address'],
        componentRestrictions: {
            'country': 'IT'
        }
    };
    zip = $('#residence_zip');
    input = document.getElementById('autocomplete');
    initAutocomplete();
});

function initAutocomplete() {
    var autocomplete = new google.maps.places.Autocomplete(input, options);
    autocomplete.addListener('place_changed', function () {
        var element = $(autocomplete.getPlace().adr_address);
        var cap = jQuery(element[2]).text();
        zip.val(cap);
    });
}

It works well! but i would like to add a filter by city name. I'm trying like this:

options = {
        types: ['address'],
        componentRestrictions: {
            'country': 'IT', 'locality': 'Roma'
        }
    };

But it doesn't work. :(

evan
  • 5,443
  • 2
  • 11
  • 20
Diego Cespedes
  • 1,353
  • 4
  • 26
  • 47
  • Possible duplicate of https://stackoverflow.com/questions/42810524/how-to-filter-address-according-country-and-city-in-google-maps-autocomplete-add – xomena Sep 29 '19 at 22:19

1 Answers1

0

It's not possible to filter Place Autocomplete predictions to one specific city only. At this time componentRestrictions can only be used to filter Autocomplete results by country.

Take a look at this open feature request in Google's Issue Tracker which I suggest starring to increase visibility and subscribe to future updates.

As a potential workaround you can try filtering results by the bounds that define the city of Roma. Also see related How to restrict google places to specific city

Hope this helps!

evan
  • 5,443
  • 2
  • 11
  • 20