| Server IP : 77.68.25.176 / Your IP : 216.73.216.24 Web Server : Apache System : Linux plesk 4.15.0-159-generic #167-Ubuntu SMP Tue Sep 21 08:55:05 UTC 2021 x86_64 User : dwp ( 10001) PHP Version : 7.4.33 Disable Function : opcache_get_status MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /var/www/vhosts/dazzlingwebplanet.co.uk/rightway-admin.dazzlingwebplanet.co.uk/public/js/ |
Upload File : |
function initAutocomplete() {
const input = document.getElementById('city');
const options = {
types: ['(cities)'],
componentRestrictions: { country: ['us', 'ca'] }
};
const autocomplete = new google.maps.places.Autocomplete(input, options);
// Optional: Event listener to capture the selected city
autocomplete.addListener('place_changed', function () {
const place = autocomplete.getPlace();
const addressComponents = place.address_components;
// Extract city name
const city = addressComponents.find(component =>
component.types.includes("locality")
)?.long_name;
const state = addressComponents.find(component =>
component.types.includes("administrative_area_level_1")
)?.long_name;
const country = addressComponents.find(component =>
component.types.includes("country")
)?.long_name;
const postalCode = addressComponents.find(component =>
component.types.includes("postal_code")
)?.long_name;
console.log({ city, state, country, postalCode });
if (city) {
// Display only the city name in the input field
$('#city').val(city);
$('#state').val(state);
$('#country').val(country);
$('#zip_code').val(postalCode);
} else {
alert("City not found!");
}
});
}
// Initialize the autocomplete when the page loads
document.addEventListener("DOMContentLoaded", initAutocomplete);