wanting to balance the work load for a site between server-side and client-side tasks, i abstracted this very simple geocoding class with Google Maps.
/*
##############################################################
Version: 1.0
Author: Abram Morphew
Created: 2011-02-10
File: class.Geocoder.php
Desc: PHP class for geocoding with Google Geocoder API. No API
key is necessary to use this class.
##############################################################
*/
if(!defined('APPNAME')) { die('Hacking attempt'); }
class Geocoder {
public $status = null;
public $address = null;
public $response = array();
public $coordinates = array();
function __construct() { }
function coordinates($address=null,$city=null,$state=null,$postcode=null,$country=null) {
$query = urlencode($address.",".$city.",".$state.",".$postcode.",".$country);
$url = "http://maps.google.com/maps/api/geocode/json?address=".$query."&sensor=false";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// set public vars and return...
$this->response = json_decode($response,true);
$this->status = $this->response['status'];
$this->address = $this->response['results'][0]['formatted_address'];
$this->coordinates = $this->response['results'][0]['geometry']['location'];
return $this->coordinates;
}
}