Tuesday, November 19, 2024
Google search engine
HomeLanguagesCountry State City using Google API

Country State City using Google API

Country state city using google apis; Through this tutorial, you will learn how to get country state city using google apis.

Country State City using Google API

Let’s use the following steps to get country state city using google apis:

  • Step 1 – Get Google API Key
  • Step 2 – Implement Code to Show Country State City
  • Step 3 – Add Google Api Key in Code
  • Step 4 – Start Country State City using Google API App

Step 1 – Get Google API Key

You need to create google map API key before you can make calls to the Google Maps Geocoding service.

First of all, you have to visit: https://cloud.google.com/maps-platform/?_ga=2.27293823.277869946.1577356888-568469380.1576660626#get-started and get the API key.

To get an API key:

  1. Visit the Google Cloud Platform Console.
  2. Click the project drop-down and select or create the project for which you want to add an API key.
  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

Create a simple php file, which name country-state-city.php and add the following code into your file:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Display Country State City using Google API</title>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

</head>
<body>
<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" />
    </div>
</div>
</div>
</body>
</html>
 

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places&key=AIzaSyCXFJ-lc7cHHcEklG2_oIhTnPKTWsLwHEU"></script>
<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 () 
	{
		console.log(places.getPlace());
		var getaddress 	  = places.getPlace();				//alert(getaddress.address_components[0].long_name);
		var whole_address = getaddress.address_components;  //alert(whole_address + 'whole_address');   
		$('#YourCity').val('');
		$('#YourState').val('');
		$('#YourCountry').val('');
		$('#YourPinCode').val('');
		
		$.each(whole_address, function(key1, value1) 
		{
			//alert(value1.long_name);
			//alert(value1.types[0]);
			
			
			if((value1.types[0]) == 'locality')
			{
				var prev_long_name_city = value1.long_name;  
				//alert(prev_long_name_city + '__prev_long_name_city');
				$('#YourCity').val(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');
				$('#YourState').val(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');
				$('#YourCountry').val(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');
				$('#YourPinCode').val(prev_long_name_pincode);
			}
 
		});	
		
	});
});
</script>

Step 3 – Add Google Api Key in Code

In this step, you will create a javascript code for calling the google geocode v3 api with the address to get country state city name from address.

Note:- Don’t forget to put your google map api key here:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places&key=API_KEY_CODE"></script>
<script>

Then add the above-given API key code into your html code.

Step 4 – Start Country State City using Google API App

Now, open your browser and open this app into it.

Conclusion

Country state city using google apis; Through this tutorial, you have learned how to get country state city using google apis.

Recommended Tutorials

RELATED ARTICLES

Most Popular

Recent Comments