<?php
/**
* address_elevation
*
* Returns the elevation (in meters) above sea level for an address.
*
* @version 0.2
* @author Contributors at eXorithm
* @link /algorithm/view/address_elevation Listing at eXorithm
* @link /algorithm/history/address_elevation History at eXorithm
* @license /home/show/license
*
* @param mixed $address The address to get the elevation for.
* @return mixed
*/
function address_elevation($address='Denver, Colorado')
{
// get the lat/long for this address
$data = file_get_contents("http://maps.google.com/maps/geo?output=csv&q=".urlencode($address));
$arr = explode(",", $data);
if (count($arr)>=4) {
if ($arr[0]==200) {
// get the elevation for this lat/long
$data = file_get_contents("http://maps.googleapis.com/maps/api/elevation/xml?sensor=false&locations=".$arr[2].','.$arr[3]);
$obj = simplexml_load_string($data);
if ($obj instanceof SimpleXMLElement) {
$obj = (array) $obj;
$obj = $obj['result'];
if ($obj instanceof SimpleXMLElement) {
$obj = (array) $obj;
return $obj['elevation'];
} else {
throw new Exception('Elevation lookup failed');
}
} else {
throw new Exception('Elevation lookup failed');
}
} else {
throw new Exception('Address lookup failed');
}
} else {
throw new Exception('Address lookup failed');
}
}
?>