<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBcRA4LijSY8e-FG4lN1sTYChasKAEsrGo&libraries=places"></script>
</head>
<body>
<input type="text" id="autocomplete-input" placeholder="Enter a location">
</body>
<script>
// Replace 'YOUR_API_KEY' with your actual API key
const apiKey = 'AIzaSyBcRA4LijSY8e-FG4lN1sTYChasKAEsrGo';
// Initialize the autocomplete object
const autocomplete = new google.maps.places.Autocomplete(document.getElementById('autocomplete-input'), {
types: ['geocode'], // Restrict the results to geographical locations
apiKey: apiKey // Provide your API key
});
// Add an event listener for when a place is selected
autocomplete.addListener('place_changed', onPlaceSelected);
// Handle the selected place
function onPlaceSelected() {
const place = autocomplete.getPlace();
console.log(place); // You can access the selected place object here
}
</script>
</html>
Comments
Post a Comment