예제 #1
0
function getCity($string)
{
    if (($response = geocodeJSON($string)) == null) {
        return null;
    }
    $city = getCityFromGeocodeResponse($response);
    $state = getStateFromGeocodeResponse($response);
    return "{$city}, {$state}";
}
예제 #2
0
 private static function getGeocodeJSON($location)
 {
     $geocode = geocodeJSON($location);
     if (is_null($geocode)) {
         return null;
     }
     // Store the geocode for the location.
     GeocodeModel::record($location, $geocode);
     return $geocode;
 }
예제 #3
0
function geocodeJSON($string, $attempts = 0)
{
    $string = str_replace(" ", "+", urlencode($string));
    $details_url = "https://maps.googleapis.com/maps/api/geocode/json?address=" . $string . "&sensor=false&key=AIzaSyBir2K0y4wPMmcMNKElShQFlb95fvvYd8E";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $details_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $response = json_decode(curl_exec($ch), true);
    curl_close($ch);
    // If Status Code is ZERO_RESULTS, OVER_QUERY_LIMIT, REQUEST_DENIED or INVALID_REQUEST
    if ($response['status'] != 'OK') {
        if ($response['status'] == 'OVER_QUERY_LIMIT' && $attempts < 3) {
            sleep(2);
            return geocodeJSON($string, $attempts + 1);
        }
        return null;
    }
    return $response;
}
예제 #4
0
function getCity($string)
{
    if (($response = geocodeJSON($string)) == null) {
        return null;
    }
    $address_components = $response['results'][0]['address_components'];
    $city = null;
    $state = null;
    foreach ($address_components as $c) {
        if (in_array("locality", $c['types'])) {
            $city = $c['short_name'];
        }
        if (in_array("administrative_area_level_1", $c['types'])) {
            $state = $c['short_name'];
        }
    }
    if ($city == null or $state == null) {
        return null;
    }
    return "{$city}, {$state}";
}