简体   繁体   中英

Limit google autocomplete to a postal code

Trying to work with the Google Autocomplete without maps, but it's being troublesome.

<script>
  var autocomplete;
  var countryRestrict = {country: 'NO', postalCode: '0365'}
  function initAutocomplete() {
    autocomplete = new google.maps.places.Autocomplete(
      (document.getElementById('address-field')),
      { types: ['address'],
        componentRestrictions: countryRestrict
      });
  }
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=API-KEY&libraries=places&callback=initAutocomplete"
        async defer></script>

when I add postalCode to countryRestrict it stops working, yet I see in their own example that they seem to be restricting it by using the example:

componentRestrictions: {country: "AU", postalCode: "2000"}

https://developers.google.com/maps/documentation/javascript/examples/geocoding-component-restriction

Is it possible for me to limit the address autocomplete to a given postal code?

Currently it's not possible, the only supported componentRestriction for an Autocomplete is country (The linked example is an implementation of geocoding, where more componentRestrictions are supported )

Adding other componentRestrictions than the supported options results in a script-error which breaks the Autocomplete

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM