public function GoogleGeocode::write in geoPHP 8
Same name and namespace in other branches
- 7 geoPHP/lib/adapters/GoogleGeocode.class.php \GoogleGeocode::write()
Serialize geometries into a WKT string.
Parameters
Geometry $geometry:
string $return_type Should be either 'string' or 'array':
Return value
string Does a reverse geocode of the geometry
Overrides GeoAdapter::write
File
- geoPHP/
lib/ adapters/ GoogleGeocode.class.php, line 92
Class
- GoogleGeocode
- PHP Google Geocoder Adapter
Code
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;
}
}
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;
}
}