How to use Google Map Geocoding API in PHP

Google Map API has geocoding technique from which we can get information about latitude/longitude, complete address information like region, city, country etc.. of any address. We can also perform reverse geocoding by passing the latitude and longitude of a place and then can get its address and other additional information. Sometimes, we may need to use google map api in  PHP instead of javascript.

So, whenever you need to use Google Map API’s geocoding service, you can use it by sending request to this url https://maps.googleapis.com/maps/api/geocode/json?address={your_address}&key={#YOUR_API_KEY} using curl method of PHP as :

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, ‘https://maps.googleapis.com/maps/api/geocode/json?address=’ . rawurlencode($address).’&key={#YOUR_API_KEY}’;
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($curl);
curl_close ($curl);
$arr = json_decode($json, true);
if($arr[‘status’]==’OK’){
echo ‘Latitude : ‘. $arr[‘results’][0][‘geometry’][‘location’][‘lat’].'<br>’;
echo ‘Longitude : ‘. $arr[‘results’][0][‘geometry’][‘location’][‘lng’];
}

Thus, you can easily use Google Map Geocoding Service in PHP also.

Leave a Reply