Created
October 28, 2012 00:15
-
-
Save lukelex/3966992 to your computer and use it in GitHub Desktop.
Get the zip code state from Google Api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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