Example #1
0
 /**
  * Serialize geometries into a WKT string.
  *
  * @param Geometry $geometry
  * @param string $return_type Should be either 'string' or 'array'
  *
  * @return string Does a reverse geocode of the geometry
  */
 public function write(Geometry $geometry, $return_type = 'string')
 {
     $centroid = $geometry->getCentroid();
     $lat = $centroid->getY();
     $lon = $centroid->getX();
     $url = "http://maps.googleapis.com/maps/api/geocode/json";
     $url .= '?latlng=' . $lat . ',' . $lon;
     $url .= '&sensor=false';
     $this->result = json_decode(@file_get_contents($url));
     if ($this->result->status == 'OK') {
         if ($return_type == 'string') {
             return $this->result->results[0]->formatted_address;
         }
         if ($return_type == 'array') {
             return $this->result->results[0]->address_components;
         }
     } elseif ($this->result->status == 'ZERO_RESULTS') {
         if ($return_type == 'string') {
             return '';
         }
         if ($return_type == 'array') {
             return $this->result->results;
         }
     } else {
         if ($this->result->status) {
             throw new Exception('Error in Google Reverse Geocoder: ' . $this->result->status);
         } else {
             throw new Exception('Unknown error in Google Reverse Geocoder');
         }
         return FALSE;
     }
 }