-1

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?

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266

2 Answers2

4

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;
}
geocodezip
  • 158,664
  • 13
  • 220
  • 245
0

You can also do this, as the formatted_address doesn't give full address in some cases.

completeAddress = document.getElementById('autocomplete').value;

Where 'autocomplete' is the Id of the input field for searching for places

John David
  • 752
  • 2
  • 9
  • 21