Skip to content

Instantly share code, notes, and snippets.

@microdesign
Created November 27, 2015 10:17
Show Gist options
  • Save microdesign/b0fcfe1badda0b7c2f69 to your computer and use it in GitHub Desktop.
Save microdesign/b0fcfe1badda0b7c2f69 to your computer and use it in GitHub Desktop.
PHP Server side Google Maps Geocoding
public function geocode(array $address)
{
$address = urlencode(implode(', ', $address));
$url = sprintf('https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=%s', $address, 'YOUR-KEY');
$data = @file_get_contents($url);
$data = json_decode($data, true);
// If the json data is invalid, return empty array
if ($data['status'] != 'OK') return array();
return [
'lat' => $data["results"][0]["geometry"]["location"]["lat"],
'lng' => $data["results"][0]["geometry"]["location"]["lng"],
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment