Hello geeks, today i am going to explain about how to get lat & long of a given address using Google geocode api.
To convert an address into latitude and longitude is known as geocoding
Here you go:
// set geocoding url
$geocoding_url = 'http://maps.googleapis.com/maps/api/geocode/json';
// set address here
$address = "IIP Dehradun";
// create url to hit
$curl_url = $geocoding_url.'?address=' . urlencode($address) . '&sensor=false';
// create a new cURL resource
$ch = curl_init();
// set URL
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// grab URL and pass it to the browser; $result contains the output string
$response = curl_exec($ch);
// result from querying URL. Will parse as json
$response = json_decode($response, true);
// close cURL resource, and free up system resources
curl_close($ch);
if ($response['status'] != 'OK') {
echo 'An error has occured: ' . print_r($response);
}
else {
$geometry = $response['results'][0]['geometry'];
$latitude = $geometry['location']['lat'];
$longitude = $geometry['location']['lng'];
}
0 Comment(s)