Beispiel #1
0
 public static function getCity($location)
 {
     $geocode = GeocodeModel::lookup($location);
     if (is_null($geocode)) {
         // Geocode was not in database, so perform Geocoding API request.
         $geocode = self::getGeocodeJSON($location);
         if (is_null($geocode)) {
             return null;
         }
     }
     $geocode = new Geocode($geocode);
     $address_components = $geocode->getAddressComponents();
     $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 (is_null($city) || is_null($state)) {
         return null;
     }
     return "{$city}, {$state}";
 }