Archive for December, 2009

/*And God Said:*/ World mWorld = new World();

Saturday, December 12th, 2009

Ok so blogging isn’t really up there with my priorities. This just means I write more less often.

My semester at Uni is nearly over, only a couple of weeks left followed by a short break and i’ll be starting the next semester. The most interesting thing i’ve learned so far this year Object Orientatation. When I started my job at 3DPixel.net my first project was using PHP and OO techniques and I can honestly say I didn’t understand most of it at first but now it all makes sense.

I’ve started on a new personal project (http://rentopoly.com) to pass the time (when I have free time) which is a website targetted at students and landlords looking to rent properties. I will be using my new found knowledge of OO to build the website.

I have so far created one object which I think could be interesting for other people to use. This object connects to Yahoo! MapService API to resolve an address (via Curl) into geo latitude and longitude data. This is usefull if you need addressing lookup without having to purchase data like that supplied by the Post Office (PAF File).

To use the object;

$myAddress = new Address("House and Street Name", "Locality", "City Town", "Postcode");
echo $myAddress->getLatitude();
echo $myAddress->getLongitude();
/* You could also use the object to display address information */
echo $myAddress->getAddress1();
echo $myAddres->getCity();

You can use this with GoogleMaps to place a marker on a map.

Full object source.

 /*
 * Creates an address object
 * See - http://eighty-six.co.uk/blog/2009/12/oo-techniques/
 * @author Damon Skelhorn 
 */
class Address {

	private $postcode;
	private $address1;
	private $address2;
	private $city;
	private $longitude;
	private $latitude;
	private $url = "http://local.yahooapis.com/MapsService/V1/geocode?appid=yourapikey";

	function __construct($address1, $address2, $city, $postcode){
		$this->setAddress1($address1);
		$this->setAddress2($address2);
		$this->setCity($city);
		$this->setPostcode($postcode);
		$this->resolveAddress();
	}

	private function resolveAddress() {
		$stream = "location=" . $this->getAddress1() . ",".$this->getCity() . "," . $this->getPostcode() . '&output=php';
		$res = $this->fetchPage($this->getUrl(), TRUE, $stream, '');
		$res = unserialize($res);
		$this->setLongitude($res['ResultSet']['Result']['Longitude']);
		$this->setLatitude($res['ResultSet']['Result']['Latitude']);
	}

	private function fetchPage($url, $ispost, $params, $auth){
		if(!$ispost && $params) $url .= '?' . $params; //if not post and params (ie, if it's a get)
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		if(is_array($auth)){
			curl_setopt($ch, CURLOPT_USERPWD, $auth['username'] . ':' . $auth['password']);
			curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		}
		if($ispost){
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
		}
		$result = curl_exec($ch);
		curl_close($ch);
		return $result;
	}

    public function getAddress1() {
        return $this->address1;
    }

    private function setAddress1($address1) {
        $this->address1 = $address1;
    }

    public function getAddress2() {
        return $this->address2;
    }

    private function setAddress2($address2) {
        $this->address2 = $address2;
    }

    public function getCity() {
        return $this->city;
    }

    private function setCity($city) {
        $this->city = $city;
    }

    public function getPostcode() {
        return $this->postcode;
    }

    private function setPostcode($postcode) {
        $this->postcode = $postcode;
    }

    private function getUrl() {
        return $this->url;
    }

    private function setUrl($url) {
        $this->url = $url;
    }

    public function getLatitude() {
        return $this->latitude;
    }

    private function setLatitude($latitude) {
        $this->latitude = $latitude;
    }

    public function getLongitude() {
        return $this->longitude;
    }

    private function setLongitude($longitude) {
        $this->longitude = $longitude;
    }
}
?>