< ?php

	/* lookup.php
	*  Created 2012 by Michael Giddens / www.SilverBiology.com
	*
	* This library is free software; and provided under a
	* Copyright: Creative Commons Attribution-ShareAlike license.
	*
	* This library is distributed in the hope that it will be useful,
	* but WITHOUT ANY WARRANTY; without even the implied warranty of
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	* Library General Public License for more details.
	*
	*	Arguments:
	*
	*	cmd=findCounty
	*	DecimalLatitude (Decimal)
	*	DecimalLongitude (Decimal)
	*	callback (String) (Optional)
	*	id (string) (Optional) - If you would like to send an id it will be returned in the json string on the response.
	*
	*/

	header('Content-type: application/json');

	if ($_REQUEST['cmd'] == "findAlt" ) {
		$data = json_decode( file_get_contents( sprintf("http://ws.geonames.org/gtopo30JSON?lat=%s&lng=%s", $_REQUEST['DecimalLatitude'], $_REQUEST['DecimalLongitude'] ) ) );
		print $data->gtopo30;
	}

	if ($_REQUEST['cmd'] == "findPointDetails" ) {
		$data = json_decode( file_get_contents( sprintf("http://ws.geonames.org/gtopo30JSON?lat=%s&lng=%s", $_REQUEST['DecimalLatitude'], $_REQUEST['DecimalLongitude'] ) ) );
		$alt = $data->gtopo30;
		$data = json_decode( file_get_contents( sprintf("http://maps.google.com/maps/geo?output=json&oe=utf-8&q=%s,%s", $_REQUEST['DecimalLatitude'], $_REQUEST['DecimalLongitude'] ) ) );
		$data->altitude = $alt;
		header('Content-type: application/json');
		print json_encode($data);
	}
	
	if ($_REQUEST['cmd'] == "findCounty" ) {
		$data = json_decode( file_get_contents( sprintf("http://maps.googleapis.com/maps/api/geocode/json?sensor=false&latlng=%s,%s", $_REQUEST['DecimalLatitude'], $_REQUEST['DecimalLongitude'] ) ) );
		if (count( $data->results ) ){
			foreach( $data->results as $point ) {
				if (count($point->address_components)) {
				foreach($point->address_components as $part) {
					switch($part->types[0]) {
						case 'locality':
							$City = $part->long_name;
							break;
						case 'administrative_area_level_2':
							$County = $part->long_name;
							break;
						case 'administrative_area_level_1':
							$State = $part->long_name;
							break;
						case 'country':
							$CountryNameCode = $part->short_name;
							$CountryName = $part->long_name;
							break;
							
					}
				}
				}

				$id = $_REQUEST['id'];

				$data = json_decode( file_get_contents( sprintf("http://ws.geonames.org/gtopo30JSON?lat=%s&lng=%s", $_REQUEST['DecimalLatitude'], $_REQUEST['DecimalLongitude'] ) ) );
				
				$alt = $data->gtopo30;

				switch( strtolower($_REQUEST['output']) ) {
					case 'callback':
						print $_REQUEST['callback'] . '(' . json_encode( array( 'success' => true, 'id' => $id, 'DecimalLatitude' => $_REQUEST['DecimalLatitude'], 'DecimalLongitude' => $_REQUEST['DecimalLongitude'], 'CountryNameCode' => $CountryNameCode, 'CountryName' => $CountryName, 'State' => $State, 'County' => $County, 'Alt' => $alt ) ) . ')';
						break;
					case 'rest':
						switch( strtolower($_REQUEST['output_field']) ) {
							case 'country':
								print $CountryName;
								break;
							case 'state':
								print $State;
								break;
							default:
								print $County;
								break;
						}
						break;
					default:
						print json_encode( array( 'success' => true, 'id' => $id, 'DecimalLatitude' => $_REQUEST['DecimalLatitude'], 'DecimalLongitude' => $_REQUEST['DecimalLongitude'], 'CountryNameCode' => $CountryNameCode, 'CountryName' => $CountryName, 'State' => $State, 'County' => $County, 'Alt' => $alt ) );
						break;
				}
				break;
			}
		}	else {
			print json_encode( array( 'success' => false ) );
		}	
	}
? >