Search code examples
javascriptgoogle-mapsautocompletegoogle-maps-autocomplete

Google Maps autocomplete full address


I have a web application using Google Maps autocomplete in an input field like in the examples page:

However, they use the autocomplete to divide information and fill a form:

function fillInAddress() {
  // Get the place details from the autocomplete object.
  var place = autocomplete.getPlace();

  for (var component in componentForm) {
    document.getElementById(component).value = '';
    document.getElementById(component).disabled = false;
  }

  // Get each component of the address from the place details
  // and fill the corresponding field on the form.
  for (var i = 0; i < place.address_components.length; i++) {
    var addressType = place.address_components[i].types[0];
    if (componentForm[addressType]) {
      var val = place.address_components[i][componentForm[addressType]];
      document.getElementById(addressType).value = val;
    }
  }
}

This is not what I am looking for. What I need is a simple javascript variable with the content of that input field. Something like var completeAddress = autocomplete.getFullAddress(), but this does not exist, and I do not see a way of doing this without complex loops and arrays.

How can I obtain the text that the user selected?


Solution

  • You want the formatted_address

    function fillInAddress() {
      // Get the place details from the autocomplete object.
      var place = autocomplete.getPlace();
      document.getElementById('fulladdress').value = place.formatted_address;
    }