Skip to content

Instantly share code, notes, and snippets.

@funkjedi
Created November 29, 2012 14:40
Show Gist options
  • Save funkjedi/4169508 to your computer and use it in GitHub Desktop.
Save funkjedi/4169508 to your computer and use it in GitHub Desktop.
Geocode address
<?php
function geocode_address($address) {
$output = array('address' => $address);
$response = json_decode(file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=" . urlencode($address) . "&sensor=false"));
switch ($output['status'] = $response->status) {
case 'OK':
$output['latitude'] = $response->results[0]->geometry->location->lat;
$output['longitude'] = $response->results[0]->geometry->location->lng;
$output['location_type'] = $response->results[0]->geometry->location_type;
break;
case 'ZERO_RESULTS':
case 'OVER_QUERY_LIMIT':
case 'REQUEST_DENIED':
case 'INVALID_REQUEST':
default:
error_log("Geolocation for '$address' failed with a status of %status.");
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment