How to get latitude and longitude from google API?
1) Using Curl
$address = urlencode('858 King Georges Road, South Hurstville, New South Wales, Australia');
$details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $address . "&sensor=false";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $details_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$geoloc = json_decode(curl_exec($ch), true);
echo $latitude = $geoloc['results'][0]['geometry']['location']['lat'];
echo $longitude = $geoloc['results'][0]['geometry']['location']['lng'];
2) Using file_get_contents
$address = urlencode('858 King Georges Road, South Hurstville, New South Wales, Australia');
$details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $address . "&sensor=false";
$coordinates = json_decode($coordinates,true);
echo $latitude = $coordinates['results'][0]['geometry']['location']['lat'];
echo $longitude = $coordinates['results'][0]['geometry']['location']['lng'];
Comments
Post a Comment