All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Get Address,Longitude And Latitude Using PHP And Google Map API

Last Updated : Jul 1, 2023

IN - PHP HTML | Written & Updated By - Riya

In this tutorial we are going to show you how to get address with the help of longitude and latitude. And get longitude and latitude with the help of address using PHP and Google Map API.You may also like Create RSS Feed Using PHP

Get Address,Longitude And Latitude Using PHP And Google Map API

To Get Address,Longitude And Latitude it takes only two steps:-

  1. Make a HTML file and define markup
  2. Make a PHP file to get the data

Step 1. Make a HTML file and define markup

We make a HTML file and save it with a name address.html

<html>
<head>
</head>
<body>

<form method="post" action='get_data.php'>
  <p>Enter Your Street name,City state,Country</p>
  <textarea name='address' placeholder='Street name,City state,Country'></textarea>
  <input type="submit" name="submit_address" value="Get Coordinates">
</form>

<form method="post" action='get_data.php'>
  <p>Enter Latitude</p>
  <input type='text' name='latitude' placeholder='Enter Latitude'>
  <p>Enter Longitude</p>
  <input type='text' name='longitude' placeholder='Enter Longitude'>
  <input type="submit" name="submit_coordinates" value="Get Address">
</form>

</body>
</html>

In this step we made two forms 1st one to get longitude and latitude from address and second one is to get address using longitude and latitude. You may also like Reset Password Using PHP

Step 2. Make a PHP file to get the data

We make a PHP file and save it with name get_data.php.

<?php
if(isset($_POST['submit_address']))
{
  $address =$_POST['address']; // Google HQ
  $prepAddr = str_replace(' ','+',$address);
  $geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
  $output= json_decode($geocode);
  $latitude = $output->results[0]->geometry->location->lat;
  $longitude = $output->results[0]->geometry->location->lng;
	
  echo "latitude - ".$latitude;
  echo "longitude - ".$longitude;
}

if(isset($_POST['submit_coordinates']))
{
  $lat=$_POST['latitude'];
  $long=$_POST['longitude'];
	
  $url  = "http://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat.",".$long."&sensor=false";
  $json = @file_get_contents($url);
  $data = json_decode($json);
  $status = $data->status;
  $address = '';
  if($status == "OK")
  {
	echo $address = $data->results[0]->formatted_address;
  }
  else
  {
	echo "No Data Found Try Again";
  }
}
?>

In this step we made two isset conditions.In first condition we get longitude and latitude by using address entered by the user using Google Map API and in second condition we get address by using coordinates entered by the user.

You may also like get user location using ip address in PHP.

Thats all, this is how to To Get Address,Longitude And Latitude using PHP and Google Map API.

You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

I hope this tutorial on get current location latitude longitude using php helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪