How to Get Country State City Using Google API in PHP?

Dec 10, 2022



How to Get Country State City Using Google API in PHP?

Hi Dev,

Using Google apis to obtain nation, state, and city information; this tutorial will teach you how to do so.

Step 1: Get Google API Key

Before making calls to the Google Maps Geocoding service, you must create a Google Map API key.

1. Visit the Google Cloud Platform Console.

2. To add an API key to a project, click the project drop-down and choose or create it.

3. Click the menu button and select APIs & Services > Credentials.

4. On the Credentials page, click Create credentials > API key.The API key created dialog displays your newly created API key.

5. Click Close.The new API key is listed on the Credentials page under API keys.(Remember to restrict the API key before using it in production.)

Step 2: Implement Code to Show Country State City


<!DOCTYPE html>



<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Display Country State City using Google API</title>

<link rel="stylesheet" href="" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

<script src=""></script>

<script src="" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>

<script src="" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>



<div class="alert alert-success" role="alert">

<div class="form-group">

<label class="control-label col-sm-2" for="pwd">Address:</label>

<div class="col-sm-8">

<input type="text" id="YourPlaces" name="YourPlaces" />

<input type="text" id="YourCity" name="YourCity" placeholder="Your City" />

<input type="text" id="YourState" name="YourState" placeholder="Your State" />

<input type="text" id="YourCountry" name="YourCountry" placeholder="Your Country" />

<input type="text" id="YourPinCode" name="YourPinCode" placeholder="Your Pin Code" />






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


google.maps.event.addDomListener(window, 'load', function ()


var places = new google.maps.places.Autocomplete(document.getElementById('YourPlaces'));

google.maps.event.addListener(places, 'place_changed', function ()



var getaddress = places.getPlace(); //alert(getaddress.address_components[0].long_name);

var whole_address = getaddress.address_components; //alert(whole_address + 'whole_address');





$.each(whole_address, function(key1, value1)




if((value1.types[0]) == 'locality')


var prev_long_name_city = value1.long_name;

//alert(prev_long_name_city + '__prev_long_name_city');



if((value1.types[0]) == 'administrative_area_level_1')


var prev_long_name_state = value1.long_name;

//alert(prev_long_name_state + '__prev_long_name_state');



if((value1.types[0]) == 'country')


var prev_long_name_country = value1.long_name;

//alert(prev_long_name_country + '__prev_long_name_country');



if((value1.types[0]) == 'postal_code')


var prev_long_name_pincode = value1.long_name;

//alert(prev_long_name_pincode + '__prev_long_name_pincode');







Step 3: Add Google Api Key in Code

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

I hope it could help you...