Location: PHPKode > scripts > Search results retriever > class.geocodeRetriever.php
<?php
/*
* Copyright (C) 2010 Pierre-Luc Germain (plger)
* See class.httpRetriever.php for more information.
*/

class geocodeRetriever extends httpRetriever {
	public $allowed_params = array('address');

	public function help(){
		echo '<pre> Sample usage:
$retriever = createHttpRequest("geocode");
$coord = $retriever->fetch_results($address);
</pre>';		
	}

	public function fetch_results($address=false){
		if(!$address)	$address = isset($this->params['address'])?$this->params['address']:false;
		if(!$address)	return false;
		$url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=".urlencode($address);

		$content = $this->retrieve_page($url);	
		if(!$content)	return false;

		$json = new Services_JSON();
		$json = $json->decode($content);

		if(!$json){
			$this->doDebug(true,'Could not decode JSON.', false, __FILE__,__LINE__);
			return false;
		}

		$itemlist = array();

		if($json->status == 'OK'){
			$res = $json->results[0];
			$coord = array();
			$coord['lat'] = $res->geometry->location->lat;
			$coord['lng'] = $res->geometry->location->lng;
			return $coord;
		}
		
		$this->doDebug(true,'Could not find geolocation.', false, __FILE__,__LINE__);
		return false;			
		
	}

}
Return current item: Search results retriever