Skip to content

Instantly share code, notes, and snippets.

@bytefade
Last active January 4, 2016 18:32
Show Gist options
  • Save bytefade/652ce4cf071b168e21ec to your computer and use it in GitHub Desktop.
Save bytefade/652ce4cf071b168e21ec to your computer and use it in GitHub Desktop.
Geocode Service via CURL
<?php
if (!function_exists('geocodeService')) {
function geocodeService($endereco)
{
$endereco = str_replace (" ", "+", urlencode($endereco));
$details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$endereco;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $details_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);
// If Status Code is ZERO_RESULTS, OVER_QUERY_LIMIT, REQUEST_DENIED or INVALID_REQUEST
if ($response['status'] != 'OK') {
return null;
}
$geometry = $response['results'][0]['geometry'];
$longitude = $geometry['location']['lng'];
$latitude = $geometry['location']['lat'];
$array = array(
'latitude' => $geometry['location']['lat'],
'longitude' => $geometry['location']['lng'],
);
return $array;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment