Yahoo GeoLocation API
The Yahoo Maps API is the best API I've found for using on in internal site. Google won't let me access theirs from a non-public site.
I messed around with their code to to Latitude/Longitude lookups on UK postcodes.
Include the API JS.
<script type="text/javascript" src="http://api.maps.yahoo.com/v2.0/fl/javascript/apiloader.js"></script>
Heres the two PHP functions that do the hard work.
Make sure your /tmp is writable.
function request_cache($url, $dest_file, $timeout=43200) {
if(!file_exists($dest_file) || filemtime($dest_file) < (time()-$timeout)) {
$stream = fopen($url,'r');
$tmpf = tempnam('/tmp','YWS');
file_put_contents($tmpf, $stream);
fclose($stream);
rename($tmpf, $dest_file);
}
}
function yahoo_geo($location) {
$q = 'http://api.local.yahoo.com/MapsService/V1/geocode';
$q .= '?appid=rlerdorf&location='.rawurlencode($location);
$tmp = '/tmp/yws_geo2_'.md5($q);
request_cache($q, $tmp, 43200);
libxml_use_internal_errors(true);
$xml = simplexml_load_file($tmp);
if ($xml) {
$ret['precision'] = (string)$xml->Result['precision'];
foreach($xml->Result->children() as $key=>$val) {
if(strlen($val)) $ret[(string)$key] = (string)$val;
}
return $ret;
}
}
Here's some code that loops over a database and looks up the latitude & longitude from a UK postcode.
Obviously change to suit your requirements.
$rs = $conn->execute('SELECT postcode FROM table');
echo '<pre>';
foreach($rs as $foo){
$latlon = yahoo_geo($foo['postcode']);
print_r($latlon);
}
echo '</pre>';
Related posts
- Yahoo Term Extractor
A recent project I was working on cause me to stumble over the Yahoo Term... - Poor mans currency conversion
I recently completed a project that required currency conversion. I was limited what I could... - Truncate a string to the nearest word
Truncating a string in PHP is easy, substr($start,$finish), but its annoying when it truncates the... - [Snippet] Formatting a currency string
The Shopp ecommerce plugin for WordPress has a little annoying "feature" that returns the product... - XHTML Transitional Compliant Paging with ADOdb
Following on from my previous post, I've cleaned up the getNavigation() function to generate XHTML...



