Skip to content

Instantly share code, notes, and snippets.

@Hodzinek
Forked from ritec/zipToCityState.js
Created October 10, 2018 13:36
Show Gist options
  • Save Hodzinek/415f562faeecec278dc6534e674a0786 to your computer and use it in GitHub Desktop.
Save Hodzinek/415f562faeecec278dc6534e674a0786 to your computer and use it in GitHub Desktop.
Get City and State from Zip Code with Google Geo API
window.findAddressFromZip = function(zipcode) {
var city, state, zip;
zip = zipcode.value;
city = '';
state = '';
if (zip.length === 5) {
$.ajax({
type: 'POST',
url: "http://maps.googleapis.com/maps/api/geocode/json?address=" + zip + "?key=XXXXXXXXXXXXXXXXXXXX",
success: (function(_this) {
return function(data) {
if (data["status"] === "OK") {
$('input#user_account_attributes_address_attributes_city').val(data["results"][0]["address_components"][1]["long_name"]);
return $('select#user_account_attributes_address_attributes_state').val(data["results"][0]["address_components"][4]["long_name"]);
}
};
})(this)
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment