title: Find City, State, & County of User Inputted Zip Code (United States Zip codes only)
body: <p>I need to find the city, state, and county of a user inputted zip code or user inputted city,/state using JavaScript.</p>
  
  <p>I understand that some (very few) cities/zip codes in the United States are located in 2 or more counties.</p>
  
  <p>I am using the Google Maps Geocoding API to accomplish this but I am having issues with certain zip codes.</p>
  
  <p><strong>Works correctly for the following (and many others):</strong></p>
  
  <ul>
  <li>90210 (City: Beverly Hills State: CA County: Los Angeles County)</li>
  <li>55101 (City: Saint Paul State: MN County: Ramsey County)</li>
  <li>77001 (City: Houston State: TX County: Harris County)</li>
  </ul>
  
  <p><strong>Works incorrectly for the following (and many others):</strong></p>
  
  <ul>
  <li>24501 (Lynchburg, VA) - Does not display anything</li>
  <li>10001 (New York, NY) - Does not display anything</li>
  </ul>
  
  <p>I am making 2 requests to the Google Maps Geocoding API. The first request is to find the city and state of the user's inputted zip code. The second request takes the city and state and finds the county. I plan on using my Geocoding API key for each request but it is working fine without while testing locally.</p>
  
  <p><strong>My code is as follows:</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function submit_form() {
  
      var city;
      var state;
      var county;
  
      // Get the user's input
      var zip = document.getElementById("user_input").value;
  
      // Make a request to the Google Maps Geocoding API
      // Find city and state of the user's zip code
      $.getJSON('https://maps.googleapis.com/maps/api/geocode/json?address='+zip, function(data){
  
                var address_components = data.results[0].address_components;
  
                $.each(address_components, function(index, component){
                       var types = component.types;
  
                       $.each(types, function(index, type){
                              if(type == 'locality') {
                                  city = component.long_name;
                              }
                              if(type == 'administrative_area_level_1') {
                                  state = component.short_name;
                              }
                       });
                });
  
                callCounty();
      });
  
      // Make a request to the Google Maps Geocoding API
      // Find the county of the city and state
      function callCounty() {
          $.getJSON('https://maps.googleapis.com/maps/api/geocode/json?address='+city+state, function(data){
  
              var address_components = data.results[0].address_components;
  
                $.each(address_components, function(index, component){
                       var types = component.types;
  
                       $.each(types, function(index, type){
                              if(type == 'administrative_area_level_2') {
                                  county = component.short_name;
                                  waitToCall();
                              }
                       });
                });
          });
      }
  
      function waitToCall() {
          document.getElementById('log').innerHTML += "&lt;p&gt;&lt;strong&gt;City:&lt;/strong&gt; " + city + " &lt;strong&gt;State:&lt;/strong&gt; " + state + " &lt;strong&gt;County:&lt;/strong&gt; " + county + "&lt;/p&gt;";
      }
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;h1&gt;Find Your City, State, and County&lt;/h1&gt;
  &lt;p&gt;Enter your zip code or your city, state:&lt;/p&gt;
  &lt;form&gt;
  &lt;input type="text" name="user_input" id="user_input" placeholder="City, State, or Zip code" /&gt;
  &lt;input type="button" value="Submit" onclick="submit_form()"/&gt;
  &lt;/form&gt;
  &lt;div id="log"&gt;&lt;p&gt;Results will show here.&lt;/p&gt;&lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>