<?php
/**
* Yahoo Geocoder for US Cities
*
* @author Jan Bruyndonckx
* @copyright Copyright (c) 2008 Banzora http://software.banzora.com
* @package main
* @subpackage lib
*
*/
require_once "Geocoder.inc";
/**
Class to geocode a US location.
The location is specified using the zip code, the cityname and the state
The zip code has precedence.
We use the Yahoo geocoder described at:
http://developer.yahoo.net/maps/rest/V1/geocode.html
*/
class USGeocoder extends Geocoder
{
const SERVICE_URL = "http://api.local.yahoo.com/MapsService/V1/geocode?" ;
const APPLICATION_ID = "tripticdesign" ;
/**--------------------------------------------------------------------
Perform the query to the internet service provider.
Return NULL in case of error, or an xml text
*/
protected function createRequest () {
$url=USGeocoder::SERVICE_URL . "appid=" . USGeocoder::APPLICATION_ID ;
if ($this->zipcode != NULL)
{
$url = $url . "&zip=" . $this->zipcode ;
}
if ($this->city != NULL)
{
$url = $url . "&city=" . $this->city ;
}
if ($this->state != NULL)
{
$url = $url . "&state=" . $this->state ;
}
return $url ;
}
/**--------------------------------------------------------------------
Parses a result that looks like:
<ResultSet>
<Result precision="zip+4">
<Latitude>37.4196</Latitude>
<Longitude>-122.0298</Longitude>
<Address></Address>
<City>SUNNYVALE</City>
<State>CA</State>
<Zip>94089-1019</Zip>
<Country>US</Country>
</Result>
</ResultSet>
*/
public function parseResult ($result)
{
$dom = new DOMDocument() ;
$dom->preserveWhiteSpace = false ;
$result = $dom->loadXML ($result) ;
if ($result == false)
{
return NULL ; // $result should be valid xml
}
$latitudelist = $dom->getElementsByTagName ("Latitude") ;
if ($latitudelist->length != 1)
{
return NULL ; // location has one latitude only
}
$longitudelist = $dom->getElementsByTagName ("Longitude") ;
if ($longitudelist->length != 1)
{
return NULL ; // location has one longitude only
}
return $this->createResult ($longitudelist->item(0)->textContent,
$latitudelist->item(0)->textContent) ;
}
}
// end class USGeocoder
?>