public function GoogleGeocode::read in geoPHP 8
Same name and namespace in other branches
- 7 geoPHP/lib/adapters/GoogleGeocode.class.php \GoogleGeocode::read()
Read an address string or array geometry objects
Parameters
string - Address to geocode:
string - Type of Geometry to return. Can either be 'points' or 'bounds' (polygon):
Geometry|bounds-array - Limit the search area to within this region. For example: by default geocoding "Cairo" will return the location of Cairo Egypt. If you pass a polygon of illinois, it will return Cairo IL.
return_multiple - Return all results in a multipoint or multipolygon:
Return value
Overrides GeoAdapter::read
File
- geoPHP/
lib/ adapters/ GoogleGeocode.class.php, line 32
Class
- GoogleGeocode
- PHP Google Geocoder Adapter
Code
public function read($address, $return_type = 'point', $bounds = FALSE, $return_multiple = FALSE) {
if (is_array($address)) {
$address = join(',', $address);
}
if (gettype($bounds) == 'object') {
$bounds = $bounds
->getBBox();
}
if (gettype($bounds) == 'array') {
$bounds_string = '&bounds=' . $bounds['miny'] . ',' . $bounds['minx'] . '|' . $bounds['maxy'] . ',' . $bounds['maxx'];
}
else {
$bounds_string = '';
}
$url = "http://maps.googleapis.com/maps/api/geocode/json";
$url .= '?address=' . urlencode($address);
$url .= $bounds_string;
$url .= '&sensor=false';
$this->result = json_decode(@file_get_contents($url));
if ($this->result->status == 'OK') {
if ($return_multiple == FALSE) {
if ($return_type == 'point') {
return $this
->getPoint();
}
if ($return_type == 'bounds' || $return_type == 'polygon') {
return $this
->getPolygon();
}
}
if ($return_multiple == TRUE) {
if ($return_type == 'point') {
$points = array();
foreach ($this->result->results as $delta => $item) {
$points[] = $this
->getPoint($delta);
}
return new MultiPoint($points);
}
if ($return_type == 'bounds' || $return_type == 'polygon') {
$polygons = array();
foreach ($this->result->results as $delta => $item) {
$polygons[] = $this
->getPolygon($delta);
}
return new MultiPolygon($polygons);
}
}
}
else {
if ($this->result->status) {
throw new Exception('Error in Google Geocoder: ' . $this->result->status);
}
else {
throw new Exception('Unknown error in Google Geocoder');
}
return FALSE;
}
}