Autocomplete Address Search Module Using Google API and PHP

Last updated on August 31st, 2017 by Paritosh Pandey

Today I’m going to show you how you can add an autocomplete address search module in your website using Google Place API web service.

In our autocomplete address search module we have used a Jquery plugin named Select2, for giving type and search feature to our module.

Steps to implement an autocomplete address search module

First step is to grab a google’s API Key, you can find it here.


<!DOCTYPE html>
<html lang="en">
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">

 <script type="text/javascript" src=""></script>

 <link href="" rel="stylesheet" />

 <script src=""></script>


 <select id="address" style="width:500px;">

 <script type="text/javascript">

 var val = $("#address").val();

 placeholder: "Search for your city",
 ajax: {
 url: function(params){
 return 'api.php?data='+params.term; 
 dataType: 'json', 
 processResults: function (data) {
 return {
 results: $.map(data, function (item) {
 return {
 text: item.text,
 id: item.text







 $terms = $_GET['data'];

 $data = file_get_contents("".$terms."&types=geocode&key={YOUR_API_KEY}");

 $arr = array();
 foreach(json_decode($data)->predictions as $item){
 $arr[$i] = array(
 'id' => $i,
 'text' => $item->description

 echo json_encode($arr);


you just have to put above index.html and api.php in a same directory, and you replac {YOUR_API_KEY} with your’s one.

Important Note

By using google place api google simply needs some policy to be follow (read this policy here), google wants their “powered by google” logo to be shown in the dropdown.

Note :- you need a live or local server to run this module.

To get latest updates please subscribe us and like our page, we are going to post lots of interesting stuffs in future.

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *