Hello All,
In this blog we will discuss about how to fetch the lat/long from the given address in PHP.
So we will be using curl request for fetching the same via using Google maps api.
Below is the code, you can use to generate lat/long :
public function getLatLng($add=null)
{
$result_val = '';
$response = array();
if(!empty($add))
{
$address = str_replace(" ", ",+", $add);
}
else
{
$address = '+Illinois,United+States';
}
$strAdd = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $address . "&sensor=false";
$ch = curl_init($strAdd);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$result = json_decode($output, true);
$result_val = $result['results'][0]['geometry']['location'];
$lat_val = $result['results'][0]['geometry']['location']['lat'];
$lng_val = $result['results'][0]['geometry']['location']['lng'];
$response['lat'] = $lat_val;
$response['long'] = $lng_val;
return $response;
}
0 Comment(s)