Ask Question

Menu

How to get address from latitude longitude in PHP


index.php

<!DOCTYPE html>
<html>
<body>
<form method="post" action="process.php">
Latitude:<br>
<input type="text" name="latitude">
<br>
Longitude:<br>
<input type="text" name="longitude">
<br>
<br>
<input type="submit" value="submit">
</form>
</body>
</html>

process.php

<?php
function getaddress($lat,$lng)
{
$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lng).'&sensor=false';
$json = @file_get_contents($url);
$data=json_decode($json);
$status = $data->status;
if($status=="OK") return $data->results[0]->formatted_address;
else
return false;
}
$lat=$_POST['latitude'];
$lng=$_POST['longitude'];
$address= getaddress($lat,$lng);
if($address)
{
echo $address;
}
else
{
echo "Not found";
}
?>










Subscribe