title: Parsing address_components in Google Maps upon autocomplete select
body: <p>I have the following code to parse the <code>country</code> when the autocomplete list is selected:</p>
  
  <pre><code>$('#spot_address').autocomplete({
    // This bit uses the geocoder to fetch address values
    source: function(request, response) {
      geocoder.geocode( {'address': request.term }, function(results, status) {
        // Get address_components
        for (var i = 0; i &lt; results[0].address_components.length; i++)
        {
          var addr = results[0].address_components[i];
          var getCountry;
          if (addr.types[0] == 'country') 
            getCountry = addr.long_name;
        }
        response($.map(results, function(item) {
          return {
            label: item.formatted_address,
            value: item.formatted_address,
            latitude: item.geometry.location.lat(),
            longitude: item.geometry.location.lng(),
            country: getCountry
          }
        }));
      })
    },
  
    // This bit is executed upon selection of an address
    select: function(event, ui) {
      // Get values
      $('#spot_country').val(ui.item.country);
      $('#spot_lat').val(ui.item.latitude);
      $('#spot_lng').val(ui.item.longitude);
      var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
      marker.setPosition(location);
      map.setCenter(location);
    },
  
    // Changes the current marker when autocomplete dropdown list is focused
    focus: function(event, ui) {
      var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
      marker.setPosition(location);
      map.setCenter(location);
    }
  });
  </code></pre>
  
  <p>However, the code above doesn't work, and when the country is parsed, only the first result of the autocomplete is parsed no matter what, which is significant with the array <code>results[0]</code> because it only fetches the first result.</p>
  
  <p>I tried to move it to the <code>select</code> function, but <code>ui</code> in <code>select</code> only contains <code>formatted_address</code>, <code>longitude</code> and <code>latitude</code>, but not the <code>address_components</code>.</p>
  
  <p>What must I do to send the correct <code>country</code> when the autocomplete list item is selected?</p>
  
  <p>Many thanks.</p>