Skip to content

Instantly share code, notes, and snippets.

@oritromax
Created December 12, 2019 23:51
Show Gist options
  • Save oritromax/6e13ecf66497427da3764aa3ddb46312 to your computer and use it in GitHub Desktop.
Save oritromax/6e13ecf66497427da3764aa3ddb46312 to your computer and use it in GitHub Desktop.
$(window).on('load',function () {
var searchinput = 'street_number';
var autocomplete;
autocomplete = new google.maps.places.Autocomplete((document.getElementById(searchinput)),{
types: ['geocode']
});
autocomplete.addListener('place_changed', fillInAddress);
function fillInAddress() {
var place = autocomplete.getPlace();
console.log(place);
var data = place.address_components;
var state = data.find(data => data.types[0] == "administrative_area_level_1");
var city = data.find(data => data.types[0] == "locality");
var zip = data.find(data => data.types[0] == "postal_code");
var street = data.find(data => data.types[0] == "street_number");
var route = data.find(data => data.types[0] == "route");
$('#maphelper_state select').val(state.short_name);
$('#locality').val(city.long_name);
$('#postal_code').val(zip.long_name);
$('#street_number').val(street.long_name + ' ' + route.long_name);
}
});
// For better - https://gist.github.com/oritromax/82e5da1a2d1ae8ad32c7e3308b1fd9d5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment