Skip to content

Instantly share code, notes, and snippets.

@nielsnuebel
Created March 27, 2020 17:41
Show Gist options
  • Save nielsnuebel/01232493b97b0b640c6d6f5ac2a74dc6 to your computer and use it in GitHub Desktop.
Save nielsnuebel/01232493b97b0b640c6d6f5ac2a74dc6 to your computer and use it in GitHub Desktop.
public function geocode($address = null)
{
if (is_null($address))
return false;
// url encode the address
$address = urlencode($address);
// google map geocode api url
$url = "https://maps.googleapis.com/maps/api/geocode/json?sensor=false&components=country:de&address={$address}&key={$this->compoParams->get('maps_api', 0)}";
// get the json response
$resp_json = file_get_contents($url);
// decode the json
$resp = json_decode($resp_json, true);
// response status will be 'OK', if able to geocode given address
if ($resp['status'] == 'OK')
{
// get the important data
$geodata['lat'] = $resp['results'][0]['geometry']['location']['lat'];
$geodata['lng'] = $resp['results'][0]['geometry']['location']['lng'];
// verify if data is complete
if (isset($geodata['lat']) && !is_null($geodata['lat']) && isset($geodata['lng']) && !is_null($geodata['lng']))
{
return $geodata;
}
else
{
return false;
}
}
else
{
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment