private function getGeoLocation($address)
{
try {
$url = 'https://maps.google.com/maps/api/geocode/json';
$address = str_replace(["'", '"'], " ", $address);
$address = str_replace([" ", " "], "+", $address);
$query_array = array(
'address' => $address,
'key' => env('GOOGLE_MAP_API_KEY')
);
$query = http_build_query($query_array);
$content = file_get_contents($url . '?' . $query);
$output = json_decode($content);
$result = [];
if ($output->status == "OK") {
$result['latitude'] = $output->results[0]->geometry->location->lat;
$result['longitude'] = $output->results[0]->geometry->location->lng;
}
return $result;
} catch (\Exception $ex) {
return [];
}
}
0 comments:
Post a Comment