Skip to main content

How to use autofill google place APi(google Map APi)?

 <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

Popular posts from this blog

How to check date format in PHP?

Check date format in PHP function isCorrectDateFromat($date){     if(!empty($date)){         $dateString = $date; // Replace this with your date string         $format = "Y-m-d"; // Replace this with your expected date format         $dateTime = DateTime::createFromFormat($format, $dateString);         if ($dateTime === false) { /*             echo "The date is not in the correct format."; */         } else {             $errors = DateTime::getLastErrors();             if (empty($errors)) { /*                 echo "The date is in the correct format."; */                 return true;             } else { /*                 echo "...