I am trying to implement the google address autocomplete feature through js. My implementation is shown below. Its working fine, the only problem is while searching the address, it suggests the generic address without postcodes as well. What my system needs is the full address with postcodes. See screenshot https://prnt.sc/wi228m . Below is my current implementation
// google map and address
var autocomplete;
const componentForm = {
street_number: "short_name",
route: "long_name",
locality: "long_name",
administrative_area_level_1: "short_name",
country: "long_name",
postal_code: "short_name",
};
function googleMapInit() {
autocomplete = new google.maps.places.Autocomplete(
document.getElementById("searchAddressList"),
{ types: ["geocode"] }
);
autocomplete.setFields(["address_component"]);
autocomplete.addListener("place_changed", fillInAddress);
}
google.maps.event.addDomListener(window, 'load', googleMapInit);
function fillInAddress() {
$('#country').val('');
$('#zip').val('');
$('#province').val('');
$('#city').val('');
$('#address').val('');
const place = autocomplete.getPlace()
for (const component of place.address_components) {
const addressType = component.types[0];
if (componentForm[addressType]) {
const val = component[componentForm[addressType]];
switch(addressType) {
case 'country':
$('#country').val(val)
break;
case 'postal_code':
$('#zip').val(val)
break;
case 'administrative_area_level_1':
$('#province').val(val)
break;
case 'locality':
$('#city').val(val)
break;
case 'route':
$('#address').val(val)
break;
}
}
}
}
Could someone suggest how to just show full address?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…