-
-
Save Hodzinek/415f562faeecec278dc6534e674a0786 to your computer and use it in GitHub Desktop.
Get City and State from Zip Code with Google Geo API
This file contains hidden or 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
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