Skip to content

Instantly share code, notes, and snippets.

@beck03076
Created December 27, 2015 18:05
Show Gist options
  • Save beck03076/fe031ac598a3cbbdd886 to your computer and use it in GitHub Desktop.
Save beck03076/fe031ac598a3cbbdd886 to your computer and use it in GitHub Desktop.
function initializeAutocomplete(){
var input = document.getElementById('product_f_locality');
var options = {
types: ['(regions)'],
componentRestrictions: {country: "IN"}
};
var autocomplete = new google.maps.places.Autocomplete(input, options);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
var lat = place.geometry.location.lat();
var lng = place.geometry.location.lng();
var placeId = place.place_id;
// to set city name, using the locality param
var componentForm = {
locality: 'short_name',
};
for (var i = 0; i < place.address_components.length; i++) {
var addressType = place.address_components[i].types[0];
if (componentForm[addressType]) {
var val = place.address_components[i][componentForm[addressType]];
document.getElementById("product_f_city").value = val;
}
}
$('#product_latitude').val(lat)
$('#product_longitude').val(lng)
$('#product_f_google_place_id').val(placeId);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment