Get Longitude and Latitude from Postal Code

I have a requirement to get Longitude and Latitude from google api. I need to get these details from postal code. So I need to call google api functions to get this details.
$postcode = "your Postal Code";



// Sanitize their postcode:
$search_code = urlencode($postcode);
$url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $search_code . '&sensor=false';
$json = json_decode(file_get_contents($url));


$lat = $json->results[0]->geometry->location->lat;
$lng = $json->results[0]->geometry->location->lng;


echo "Latitude :".$lat";
echo "Longitude :".  $lng";


Type your postal code above in the code. See the demo below.
$postcode = "HA27DY";



// Sanitize their postcode:

$search_code = urlencode($postcode);
$url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $search_code . '&sensor=false';
$json = json_decode(file_get_contents($url));


$lat = $json->results[0]->geometry->location->lat;
$lng = $json->results[0]->geometry->location->lng;


echo "Latitude :".$lat";
echo "Longitude :".  $lng";

I got Following Data from my code.
Latitude :51.5797504
Longitude :-0.3640929 

0 comments:

Post a Comment

Ask anything about this Tutorial.