title: Extracting Address Types from Google Geocdoe
body: <p>Goal: Trying to geocode an address inputted by a user, and then take the info, place it into a form and input this info into a database.</p>
  
  <p>The road so far: I have somewhat successfully accomplished this task with the following code:</p>
  
  <pre><code>$(function() {
      $("#address").autocomplete({
          //This bit uses the geocoder to fetch address values
          source : function(request, response) {
              geocoder.geocode({
                  'address' : request.term
              }, function(results, status) {
                  response($.map(results, function(item) {
                      return {
                          label : item.formatted_address,
                          value : item.formatted_address,
                          latitude : item.geometry.location.lat(),
                          longitude : item.geometry.location.lng(),
                          streetNo : item.address_components[0].long_name,
                          streetName : item.address_components[1].long_name,
                          town : item.address_components[2].long_name,
                          province : item.address_components[4].long_name,
                          postal : item.address_components[6].long_name,
                          country : item.address_components[5].long_name
                      }
  
                  }));
              })
          },
          //This bit is executed upon selection of an address
          select : function(event, ui) {
  
              //Now that an address has been selected, show the address form
              $("#newReview").show('fast');
              $("#latitude").val(ui.item.latitude);
              $("#longitude").val(ui.item.longitude);
              $("#streetNo").val(ui.item.streetNo);
              $("#streetName").val(ui.item.streetName);
              $("#streetNo").val(ui.item.streetNo);
              $("#town").val(ui.item.town);
              $("#postal").val(ui.item.postal);
              $("#province").val(ui.item.province);
              var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
              marker.setPosition(location);
              map.setCenter(location);
  
              $(".correctAddress").click(function() {
                  if($(".correctAddress").val() == "yes") {
                      $("#map").hide('fast');
                  };
              });
  
              $(".inCorrectAddress").click(function() {
                  if($(".inCorrectAddress").val() == "no") {
                      $("#map").show('fast');
                  };
              });
          }
      });
  });
  </code></pre>
  
  <p><strong>NOTE</strong> I have only included the code that does the "work".</p>
  
  <p><strong>What it does right now:</strong>: A user can look for an address, they get an autocomplete suggestion, when they select an address, a "form" with some fields appears, with the address information already populated. </p>
  
  <p>The issue however, is that depending on what address ends up being selected, the information may not always match up.</p>
  
  <p>I.E. 1139 Ontario Rd, Welland ON, L3B 5E5, Canada will work fine, and the fields will populate correctly, but if the user selects a "place" or an incorrect address like: </p>
  
  <p>Cornelia Ct, 81 Cornelia St W, Smiths Falls ON etc....</p>
  
  <p>Then the info is no good because instead of a street number, which should be 81, I get the name "Cornelia Ct" because in this instance: </p>
  
  <pre><code>streetNo : item.address_components[0].long_name,
  </code></pre>
  
  <p>That array node is actually the "Cornelia Ct" name, which is the first part of the address.</p>
  
  <p>What I need to be able to do is extract the "types" given back by the geocode, as in this example: <a href="https://developers.google.com/maps/documentation/geocoding/#JSON" rel="nofollow noreferrer">https://developers.google.com/maps/documentation/geocoding/#JSON</a> 
  or the info on this link: 
  <a href="https://developers.google.com/maps/documentation/geocoding/#Types" rel="nofollow noreferrer">https://developers.google.com/maps/documentation/geocoding/#Types</a></p>
  
  <p>That way, if I can do that the street number will always be "street_number" and street name "route" etc.. etc..., allowing me to keep my data valid and uniform. </p>
  
  <p>I've checked out this question on stackoverflow: </p>
  
  <p><a href="https://stackoverflow.com/questions/8942108/how-to-return-address-types-from-google-maps-geocode">How to return address types from google maps geocode?</a> and that answer works, but it's doing the same thing I am already. </p>
  
  <p>I admittedly have little experience parsing JSON but with what I understand and have tried so far, I think there would be a way to use the actual "types" name instead of the array's node number?</p>
  
  <p>I have tried various things like:</p>
  
  <pre><code>item.address_components['street_name'].long_name
  </code></pre>
  
  <p>and variations of it but I can't seem to crack it. </p>
  
  <p>The second issue with this code is that by asking for those specific results, the auto complete search only gives you a suggestion when you have most of it typed in already, as if it's waiting to match all those fields before it guesses. </p>
  
  <p>If I could use the array outside of the "return" from geocoding theoretically it should then allow for a more fluid search, as it does if I remove: </p>
  
  <pre><code>streetNo : item.address_components[0].long_name,
  streetName : item.address_components[1].long_name,
  town : item.address_components[2].long_name,
  province : item.address_components[4].long_name,
  postal : item.address_components[6].long_name,
  </code></pre>
  
  <p>Thanks in advance for your time folks!</p>