Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine US County by zip-code?

Is it possible to implement a function on clientside in javascript to use it like this:

var county = get_county(zip_code);

I mean is it possible to do it online by script? Or I have to dig some database? Or I have to buy it?

Any help is appreciated!

P.S.
By the help of Dyrandz Famador, I did this funciton for GAS:

function get_county(zip) {
 var county, response, result, adresses, i, j, n, type;
 response = Maps.newGeocoder().geocode(zip);
 for (i = 0; i < response.results.length; i++) {
   result = response.results[i];
   adresses = result.address_components;
   n = adresses.length;
   for (j=0;j<n;j++) {
     type = adresses[j].types[0];
     if (type == 'locality') county = (adresses[j].long_name);
     if (type == 'administrative_area_level_2')   {county =(adresses[j].long_name);}
   }
 }
 return (county);
}
like image 385
Karen Fisher Avatar asked Mar 11 '15 07:03

Karen Fisher


1 Answers

you can use the Google Maps API to Get Locations from Zip Codes

Google offers many API’s, among them is the Maps API. In this example we’ll show all the code necessary to hit Google with a zip code to get the location in the form of City, State and Country.

First thing to do is to reference Google’s Map API:

<script language="javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>

Next is a little form used to enter your zip code:

<form>
zip: <input type="text" name="zip" value="46032"> <a href="#" onclick="getLocation()">Get Address</a>
</form>

You can see that I have a link which fires a function called “getLocation()” – below you’ll find that function and the related code:

...
<script language="javascript">
function getLocation(){
  getAddressInfoByZip(document.forms[0].zip.value);
}

function response(obj){
  console.log(obj);
}
function getAddressInfoByZip(zip){
  if(zip.length >= 5 && typeof google != 'undefined'){
    var addr = {};
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'address': zip }, function(results, status){
      if (status == google.maps.GeocoderStatus.OK){
        if (results.length >= 1) {
      for (var ii = 0; ii < results[0].address_components.length; ii++){
        var street_number = route = street = city = state = zipcode = country = formatted_address = '';
        var types = results[0].address_components[ii].types.join(",");
        if (types == "street_number"){
          addr.street_number = results[0].address_components[ii].long_name;
        }
        if (types == "route" || types == "point_of_interest,establishment"){
          addr.route = results[0].address_components[ii].long_name;
        }
        if (types == "sublocality,political" || types == "locality,political" || types == "neighborhood,political" || types == "administrative_area_level_3,political"){
          addr.city = (city == '' || types == "locality,political") ? results[0].address_components[ii].long_name : city;
        }
        if (types == "administrative_area_level_1,political"){
          addr.state = results[0].address_components[ii].short_name;
        }
        if (types == "postal_code" || types == "postal_code_prefix,postal_code"){
          addr.zipcode = results[0].address_components[ii].long_name;
        }
        if (types == "country,political"){
          addr.country = results[0].address_components[ii].long_name;
        }
      }
      addr.success = true;
      for (name in addr){
          console.log('### google maps api ### ' + name + ': ' + addr[name] );
      }
      response(addr);
        } else {
          response({success:false});
        }
      } else {
        response({success:false});
      }
    });
  } else {
    response({success:false});
  }
}
</script>

...

Thats it – open up the console in Chrome and notice the output as you enter different zip codes.

get it from here: http://rickluna.com/wp/2012/09/using-the-google-maps-api-to-get-locations-from-zip-codes/

like image 55
Dyrandz Famador Avatar answered Sep 28 '22 09:09

Dyrandz Famador