How can I determine US County by zip-code?
Asked Answered
F

4

14

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);
}
Farinose answered 11/3, 2015 at 7:49 Comment(2)
You can also use the SmartyStreets API.Mariann
Take a look at this article entitled How to Find a County By Using a ZIP Code.Mariann
U
9

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/

Unhopedfor answered 11/3, 2015 at 7:54 Comment(1)
The only thing I can add to this outstanding answer is the JS logic for the County name. if (types == "administrative_area_level_2,political"){ addr.county = results[0].address_components[ii].long_name; }Mastitis
L
1

you need to know the mapping of zip_code and country,

I've found a github project for this:

https://github.com/jgoodall/us-maps

once you get json mapping by instruction, you can implement the function you want.

Lagunas answered 11/3, 2015 at 7:51 Comment(2)
Thank you, but the first link census.gov/cgi-bin/geo/shapefiles2010/main unavaible. So can't carry forwardFarinose
Oddly I can visit it without problem.Lagunas
T
0

Here is a nice geographic database API from the US government:
https://www.sba.gov/about-sba/sba-performance/sba-data-store/web-service-api/us-city-and-county-web-data-api#county-state

The API yields all counties and states information, however it doesn't seem to have ZIP codes included. Using Google Geocoding API might be easier.

Tactile answered 11/3, 2015 at 7:54 Comment(2)
Looks like the link is broken :(Aude
@BenD Looks like they discontinued the service.Molten
H
0

This is one way you could grab the address, city, state and/or zip from pretty much any location input, such as Santa Monica, CA or 90405. Keep in mind you can't always get all of these variables back unless you are very specific in your request.

function getLocationInfo(){

    var location = $('#location').val();
    var geocoder = new google.maps.Geocoder();

    geocoder.geocode({ 'address': location }, function (result, status) {

    var address = '';
    var streetNumber = '';
    var streetName = '';
    var cityName = '';
    var stateName = '';
    var zipCode = '';

    var addressComponent = result[0]['address_components'];

    // find street number

    var streetNumberQueryable = $.grep(addressComponent, function (x) {
        return $.inArray('street_number', x.types) != -1;
    });

    if (streetNumberQueryable.length) {
        streetNumber = streetNumberQueryable[0]['long_name'];
    }

    // find street name

    var streetNameQueryable = $.grep(addressComponent, function (x) {
        return $.inArray('route', x.types) != -1;
    });

    if (streetNameQueryable.length) {
        streetName = streetNameQueryable[0]['long_name'];
    }

    // find city data

    var cityQueryable = $.grep(addressComponent, function (x) {
        return $.inArray('locality', x.types) != -1;
    });

    if (cityQueryable.length) {
        cityName = cityQueryable[0]['long_name'];
    }

    // find state data

    var stateQueryable = $.grep(addressComponent, function (x) {
        return $.inArray('administrative_area_level_1', x.types) != -1;
    });

    if (stateQueryable.length) {
        stateName = stateQueryable[0]['long_name'];
    }

    // find zip data

    var zipQueryable = $.grep(addressComponent, function (x) {
        return $.inArray('postal_code', x.types) != -1;
    });

    if (zipQueryable.length) {
        zipCode = zipQueryable[0]['long_name'];
    }

    address = streetNumber + ' ' + streetName;

    console.log(address);
    console.log(cityName);
    console.log(stateName);
    console.log(zipCode);

    });

}
Hearthstone answered 9/9, 2015 at 8:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.