Skip to content

Instantly share code, notes, and snippets.

@lukelex
Created October 28, 2012 00:15
Show Gist options
  • Save lukelex/3966992 to your computer and use it in GitHub Desktop.
Save lukelex/3966992 to your computer and use it in GitHub Desktop.
Get the zip code state from Google Api
var geocoder = new google.maps.Geocoder();
function getState(zipcode) {
geocoder.geocode( { 'address': zipcode}, function (result, status) {
var state = "N/A";
for (var component in result[0]['address_components']) {
for (var i in result[0]['address_components'][component]['types']) {
if (result[0]['address_components'][component]['types'][i] == "administrative_area_level_1") {
state = result[0]['address_components'][component]['short_name'];
// do stuff with the state here!
document.getElementById('state').innerHTML = state;
return;
}
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment