X

Get Latitude and Longitude from ZIP Code using Google Maps API and PHP

Today We are going to learn “Get Latitude and Longitude from ZIP Code using Google Maps API and PHP” this tutorial help in many project, i.e getting location on users address ZIP code.

For Pointing the exact location on Google Map it will always be a good idea to pass the latitude and longitude with Google Maps API. In this tutorial, you’ll know how to get latitude and longitude from ZIP code using Google Maps API in PHP.

Get Location

<?php
if(isset($_POST['submit']))
{
    $zipcode=$_POST['location'];
    $url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$zipcode."&sensor=false";
    $details=file_get_contents($url);
    $result = json_decode($details,true);
    $lat=$result['results'][0]['geometry']['location']['lat'];
    $lng=$result['results'][0]['geometry']['location']['lng'];
    echo "Latitude :" .$lat;
    echo '<br>';
    echo "Longitude :" .$lng;
}
?>

Front End Design

<!Doctype html>
<html>
	<head>
		<title>Location|techJunkGigs</title>
	</head>
	<body>
		<form method = "POST" action = "">
			<Label>Enter Your ZIP Code</Label>
			<Input type = "text" name = "location">
			<input type = "submit" name = "submit" value = "Get Location">
		</form>
	</body>
</html>

Combine Code For Getting Longitude and Latitude

<!Doctype html>
<html>
	<head>
		<title>Location|techJunkGigs</title>
	</head>
	<body>
		<form method = "POST" action = "">
			<Label>Enter Your ZIP Code</Label>
			<Input type = "text" name = "location">
			<input type = "submit" name = "submit" value = "Get Location">
		</form>
	</body>
</html>

<?php
if(isset($_POST['submit']))
{
    $zipcode=$_POST['location'];
    $url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$zipcode."&sensor=false";
    $details=file_get_contents($url);
    $result = json_decode($details,true);

    $lat=$result['results'][0]['geometry']['location']['lat'];

    $lng=$result['results'][0]['geometry']['location']['lng'];

    echo "Latitude :" .$lat;
    echo '<br>';
    echo "Longitude :" .$lng;
}
?>

Demo Image

So that’s all for this “Get Latitude and Longitude from ZIP Code using Google Maps API and PHP”  tutorial friends. We learned how to run get longitude and latitude with ZIP code . if you found it helpful then, please SHARE. Thank You

Categories: API HTML PHP
Jamaley Hussain: Hello, I am Jamaley. I did my graduation from StaffordShire University UK . Fortunately, I find myself quite passionate about Computers and Technology.
Related Post