Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

javascript - Suggest full address only on google address autocomplete

I am trying to implement the google address autocomplete feature through js. My implementation is shown below. Its working fine, the only problem is while searching the address, it suggests the generic address without postcodes as well. What my system needs is the full address with postcodes. See screenshot https://prnt.sc/wi228m . Below is my current implementation

 // google map and address
  var autocomplete;
  const componentForm = {
    street_number: "short_name",
    route: "long_name",
    locality: "long_name",
    administrative_area_level_1: "short_name",
    country: "long_name",
    postal_code: "short_name",
  };
  
  function googleMapInit() {
    autocomplete = new google.maps.places.Autocomplete(
      document.getElementById("searchAddressList"),
      { types: ["geocode"] }
    );
    
    autocomplete.setFields(["address_component"]);
    autocomplete.addListener("place_changed", fillInAddress);
  }

  google.maps.event.addDomListener(window, 'load', googleMapInit);
  
  function fillInAddress() {
    $('#country').val('');
    $('#zip').val('');
    $('#province').val('');
    $('#city').val('');
    $('#address').val('');

    const place = autocomplete.getPlace()

    for (const component of place.address_components) {
      const addressType = component.types[0];
      if (componentForm[addressType]) {
        const val = component[componentForm[addressType]];

        switch(addressType) {
          case 'country':
            $('#country').val(val)
            break;
          case 'postal_code':
            $('#zip').val(val)
            break;
          case 'administrative_area_level_1':
            $('#province').val(val)
            break;
          case 'locality':
            $('#city').val(val)
            break;
          case 'route':
            $('#address').val(val)
            break;
        }
      }
    }
  }

Could someone suggest how to just show full address?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found the solution to it and sharing here so that it could help others as well.

I just had to replace a parameter on the googleMapInit() method.

See code below:

function googleMapInit() {
    autocomplete = new google.maps.places.Autocomplete(
      document.getElementById("searchAddressList"),
      { types: ["address"],componentRestrictions: {country: 'us'} }
    );
    
    autocomplete.setFields(["address_component"]);
    autocomplete.addListener("place_changed", fillInAddress);
  }

Note the field "types", its parameter has changed. This will give you full address. I have added one more parameter to restrict the search results for US only.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...