https://nobleintentstudio.com/blog/zip-code-to-city-state-lookup/
$('#input').blur(function(){
var zip = $(this).val();
var city = '';
var state = '';
//make a request to the google geocode api
$.getJSON('https://maps.googleapis.com/maps/api/geocode/json?address='+zip+'',function(response){
//find the city and state
var address_components = response.results[0].address_components;
$.each(address_components, function(index, component){
var types = component.types;
$.each(types, function(index, type){
if(type == 'locality') {
city = component.long_name;
}
if(type == 'administrative_area_level_1') {
state = component.long_name;
}
if(type == 'country'){
country = component.long_name;
}
});
});
//pre-fill the city and state
$('#City').val(city);
$('#State').val(state);
$('#Country').val(country);
});
});
$('#input').blur(function(){
var zip = $(this).val();
var city = '';
var state = '';
//make a request to the google geocode api
$.getJSON('https://maps.googleapis.com/maps/api/geocode/json?address='+zip+'',function(response){
//find the city and state
var address_components = response.results[0].address_components;
$.each(address_components, function(index, component){
var types = component.types;
$.each(types, function(index, type){
if(type == 'locality') {
city = component.long_name;
}
if(type == 'administrative_area_level_1') {
state = component.long_name;
}
if(type == 'country'){
country = component.long_name;
}
});
});
//pre-fill the city and state
$('#City').val(city);
$('#State').val(state);
$('#Country').val(country);
});
});
No comments:
Post a Comment