How to Autocomplete Google Places in PHP
If you’ve ever used Google Maps or any location-based service, you might have noticed the convenient autocomplete feature that suggests places as you type. This functionality not only improves user experience but also ensures accurate and efficient data entry. In this blog post, we will explore how to implement Google Places autocomplete in PHP to enhance the search capabilities of your web application.
Prerequisites
Before diving into the implementation, you should have a basic understanding of PHP, HTML, and JavaScript. Additionally, you will need a Google Cloud Platform (GCP) account with billing enabled and the Places API activated.
Steps to add Autocomplete Google Places in PHP
- Setting Up Google API Key
- HTML and JavaScript Setup
- PHP Implementation (Optional)
- PHP Processing (process_autocomplete.php)
1. Setting Up Google API Key
To use Google Places API, you need an API key, which identifies your application and allows access to the service. If you don’t have one, follow these steps:
- Go to the Google Cloud Console: https://console.cloud.google.com/
- Create a new project or select an existing one.
- In the sidebar, click on “APIs & Services” > “Library“.
- Search for “Places API” and click on it.
- Click the “Enable” button to activate the Places API.
- Once enabled, navigate to “APIs & Services” > “Credentials“.
- Click on “Create credentials” and select “API key“.
- Copy the generated API key.
2. HTML and JavaScript Setup
Create an HTML file and add the following code to set up the input field and JavaScript code for autocomplete:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <!DOCTYPE html> <html> <head> <title>Google Places Autocomplete</title> </head> <body> <h1>Google Places Autocomplete</h1> <input type="text" id="place-input" placeholder="Enter a location"> <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script> <script> function initialize() { var input = document.getElementById('place-input'); var autocomplete = new google.maps.places.Autocomplete(input); } google.maps.event.addDomListener(window, 'load', initialize); </script> </body> </html> |
Replace YOUR_API_KEY
in the script tag with the API key, you obtained from the Google Cloud Console.
3. PHP Implementation (Optional)
In some cases, you might want to store and process the autocomplete results on the server side. For this purpose, we can use PHP to fetch the data sent by the autocomplete and perform additional operations.
Modify the HTML file to include a PHP script that receives the data and returns the results:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <!DOCTYPE html> <html> <head> <title>Google Places Autocomplete</title> </head> <body> <h1>Google Places Autocomplete</h1> <input type="text" id="place-input" placeholder="Enter a location"> <div id="results"></div> <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script> <script> function initialize() { var input = document.getElementById('place-input'); var autocomplete = new google.maps.places.Autocomplete(input); autocomplete.addListener('place_changed', function () { var place = autocomplete.getPlace(); if (!place.geometry) { return; } var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if (this.readyState === XMLHttpRequest.DONE && this.status === 200) { var resultsDiv = document.getElementById('results'); resultsDiv.innerHTML = this.responseText; } }; xhr.open("GET", "process_autocomplete.php?place_id=" + place.place_id, true); xhr.send(); }); } google.maps.event.addDomListener(window, 'load', initialize); </script> </body> </html> |
4. PHP Processing (process_autocomplete.php)
Create a new PHP file called process_autocomplete.php
to handle the autocomplete data processing:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php // Replace with your API key $api_key = "YOUR_API_KEY"; if (isset($_GET["place_id"])) { $place_id = $_GET["place_id"]; $details_url = "https://maps.googleapis.com/maps/api/place/details/json?placeid=$place_id&key=$api_key"; $response = file_get_contents($details_url); $data = json_decode($response, true); // Process the data as needed (e.g., extract location details) // Example: Get the formatted address $formatted_address = $data["result"]["formatted_address"]; // Return the data (e.g., formatted address) back to the frontend echo "<p>Formatted Address: $formatted_address</p>"; } ?> |
Conclusion
In this tutorial, we’ve walked through the process of implementing Google Places autocomplete in PHP. With this autocomplete feature, your users can effortlessly search for locations and have accurate and reliable results at their fingertips. Remember to manage your API keys securely and adhere to Google’s usage policies.
Enhance your web application’s search capabilities today by integrating Google Places autocomplete, providing your users with a seamless and user-friendly experience. Happy coding!