Last active
January 4, 2016 18:32
-
-
Save bytefade/652ce4cf071b168e21ec to your computer and use it in GitHub Desktop.
Geocode Service via CURL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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