Get coordinates from postcode or address using Google API


Sometimes you need to get exact location by address entered by user on your website. Google Maps API offers a service that will return exact coordinate by the request you send it. See following snippet to see how to get coordinates from postcode or address by using Google Maps API.

$address = '124 Norfolk Street, Glasgow, United Kingdom';
$response = file_get_contents('' . urlencode($address) . '&sensor=true');
$response = json_decode($response);
echo 'Latitude:' . $response->results[0]->geometry->location->lat;
echo 'Longitude:' . $response->results[0]->geometry->location->lng;

I hope you enjoy it and make useful scripts for your project!