Use componentRestrictions to just one state in Goo

2020-08-05 09:47发布

问题:

I am trying to use componentRestrictions in Google Places autocomplete. I can restrict by country, but when I try to restrict by administrative area, the whole thing crashes.

var options = {
    componentRestrictions: { administrativeArea: 'TX' }
};

var autocomplete = new google.maps.places.Autocomplete(input, options);

google.maps.event.addListener(autocomplete, 'place_changed', function () {
    var place = autocomplete.getPlace();
    if (!place.geometry) {
        return;
    }

    var address = '';
    if (place.address_components) {
        address = [
          (place.address_components[0] && place.address_components[0].short_name || ''),
          (place.address_components[1] && place.address_components[1].short_name || ''),
          (place.address_components[2] && place.address_components[2].short_name || '')
        ].join(' ');
    }

});

回答1:

I am afraid you can’t, because Google Maps only accept country as a filter.



标签: google-maps