PHP Classes
elePHPant
Icontem

File: index.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Carlos Machado  >  MapPoint PHP API  >  index.php  >  Download  
File: index.php
Role: Example script
Content type: text/plain
Description: Tests start page
Class: MapPoint PHP API
Access MapPoint Web services
Author: By
Last change:
Date: 9 years ago
Size: 18,771 bytes
 

Contents

Class file image Download
<?php
require_once('mappoint/ClassLoader.php');
session_start();

if (!isset($_SESSION['mappoint'])) {
	$_SESSION['mappoint'] = new Mappoint(constant('WSDL_URL'), constant('MP_USER'), constant('MP_PASS'));
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Mappoint PHP API</title>
<style type="text/css">
.header {text-align:center; font-weight:bold};
</style>
<script type="text/javascript" language="javascript">
var methods = [
//Common
'GetCountryRegionInfo',
'GetDataSourceInfo',
'GetEntityTypes',
'GetGreatCircleDistances',
'GetVersionInfo',
//Find
'Find',
'FindAddress',
'FindByID',
'FindByProperty',
'FindNearby',
'FindNearRoute',
'GetLocationInfo',
'ParseAddress',
'FindPolygon',
//Render
'ConvertToLatLong',
'ConvertToPoint',
'GetBestMapView',
'GetMap',
'GetLineDriveMap',
//Route
'CalculateRoute',
'CalculateSimpleRoute'
];

function addOption(val, tex, select) {
	opt = document.createElement('option');
	opt.text = tex;
	opt.value = val;
	select.options[select.options.length] = opt;
}

function removeOption(select) {
	select.remove(select.selectedIndex);
}

function fillMethods(menu, select) {
	min = 0, max = 0;

	switch (menu) {

		case 0:
		max = 4;
		break;

		case 1:
		min = 5;
		max = 13;
		break;

		case 2:
		min = 14;
		max = 18;
		break;

		case 3:
		min = 19;
		max = 20;
		break;

		default:
		return;
	}
	for (i = select.options.length - 1; i > -1; i--){
		select.options[i] = null;
	}
	addOption(-1, "Choose a method...", select);
	for (i = min; i <= max; i++) {
		addOption(i, methods[i], select);
	}
}


function js2php(data, type) {
	serialized = '';
	serialized += 'a:' + data.length + ':{';
	for (i = 0; i < data.length; i++) {
		serialized += "i:" + i + ';';
		serialized += serialize(data[i], type);
	}
	serialized += '}';
	return serialized;
}

function serialize(value, type) {
	serd_value = '';
	switch (type) {

		case 'd':
		case 'i':
		serd_value += type + ':' + value + ';';
		break;

		case 's':
		serd_value += 's:' + value.length + ':"' + value + '";';
		break;

		case 'b':
		serd_value += value ? 'b:1;' : 'b:0;';
		break;
	}
	return serd_value;
}

function setValue(data, element, type) {
	element.value = js2php(data, type);
}

function getOptionValues(opts) {
	vals = new Array(opts.length);

	for(i = 0; i < opts.length; i++) {
		vals[i] = opts[i].value;
	}
	return vals;
}

</script>
</head>
<body>
<?php 
if (isset($_POST['GetCountryRegionInfo'])) {
	$entityIDs = unserialize(urldecode(stripslashes($_POST['entities'])));
	$global = $_SESSION['mappoint'];
	$myCountryRegionInfos = null;
	try {
		$myCountryRegionInfos = $global->CommonService->GetCountryRegionInfo($entityIDs);
	} catch (SoapFault $e) {
		die($e->faultstring);
	}
	echo "<table border='1'>";
	echo "<tr class='header'><td colspan='5'><b>Country Region Infos</b></td></tr>";
	echo "<tr class='header'><td>EntityID</td><td>FriendlyName</td><td>ISO2 Code</td><td>Lat</td><td>Long</td>";
	foreach ($myCountryRegionInfos as $crInfo) {
		echo "<tr>";
		echo "<td>" . $crInfo->EntityID . "</td>";
		echo "<td>" . $crInfo->FriendlyName . "</td>";
		echo "<td>" . $crInfo->Iso2 . "</td>";
		echo "<td>" . $crInfo->LatLong->Latitude . "</td>";
		echo "<td>" . $crInfo->LatLong->Longitude . "</td>";
		echo "<tr>";
	}
	echo "</table>";
}
elseif (isset($_POST['GetDataSourceInfo'])) {
	$dataSources = unserialize(urldecode(stripslashes($_POST['dataSources'])));
	try {
		$myDataSources = $_SESSION['mappoint']->CommonService->GetDataSourceInfo($dataSources);
	} catch (SoapFault $e) {
		die($e->faultstring);
	}
	echo "<table border='1'>";
	echo "<tr class='header'><td colspan='5'><b>Data Source Infos</b></td></tr>";
	echo "<tr class='header'><td>Name</td><td>Description</td><td>Version</td><td>Capability</td>";
	foreach ($myDataSources as $ds) {
		echo "<tr>";
		echo "<td>" . $ds->Name . "</td>";
		echo "<td>" . $ds->Description . "</td>";
		echo "<td>" . $ds->Version . "</td>";
		echo "<td>" . $ds->Capability . "</td>";
		echo "<tr>";
	}
	echo "</table>";
}
elseif (isset($_POST['GetGreatCircleDistances'])) {
	$global = $_SESSION['mappoint'];

	//first point
	$latLong1 = new LatLong();
	$latLong1->Latitude = $_POST['lat1'];
	$latLong1->Longitude = $_POST['long1'];

	//second point
	$latLong2 = new LatLong();
	$latLong2->Latitude = $_POST['lat2'];
	$latLong2->Longitude = $_POST['long2'];

	$returnDistances = null;
	try {
		$returnDistances = $global->CommonService->GetGreatCircleDistances(array($latLong1, $latLong2));
		echo "Distance: " . $returnDistances[0];
	} catch (SoapFault $e) {
		die($e->faultstring);
	}
}
elseif (isset($_POST['GetLocationInfo'])) {
	$myLatLong = new LatLong();
	$myLatLong->Latitude = $_POST['lat'];
	$myLatLong->Longitude = $_POST['long'];
	
	$infoOptions = null;
	//a test with a GetInfoOptions object as a parameter
	/*$infoOptions = new GetInfoOptions();
	$infoOptions->EntityTypesToReturn = array('PopulatedPlace');
	$infoOptions->IncludeAllEntityTypes = false;*/
	$returnedLocations = null;
	try {
		$returnedLocations = $_SESSION['mappoint']->FindService->GetLocationInfo($myLatLong, $_POST['dataSource'], $infoOptions);
	} catch (SoapFault $e) {
		die($e->faultstring);
	}
	echo "<table border='1'>";
	echo "<tr class='header'><td colspan='4'>Locations Info</td></tr>";
	echo "<tr class='header'><td>EntityID</td><td>DisplayName</td><td>Lat</td><td>Long</td></tr>";
	foreach ($returnedLocations as $location) {
		echo "<tr><td>".$location->Entity->ID."</td>";
		echo "<td>".$location->Entity->DisplayName."</td>";
		echo "<td>".$location->LatLong->Latitude."</td>";
		echo "<td>".$location->LatLong->Longitude."</td></tr>";
	}
} elseif (isset($_POST['ParseAddress'])) {
	$address = null;
	$global = $_SESSION['mappoint'];
	try {
		$address = $global->FindService->ParseAddress($_POST['address'], $_POST['country']);
		$findAddressSpec  = new FindAddressSpecification();
		$findAddressSpec->InputAddress = $address;
		$findAddressSpec->DataSourceName = $_POST['dataSource'];

		$findAddressResults = $global->FindService->FindAddress($findAddressSpec);

		echo "<table border='1'>";
		echo "<tr class='header'><td colspan='4'>Found Location</td></tr>";
		echo "<tr class='header'><td>EntityID</td><td>DisplayName</td><td>Lat</td><td>Long</td></tr>";
		echo "<tr><td>".$findAddressResults->Results->FindResult[0]->FoundLocation->Entity->ID."</td>";
		echo "<td>".$findAddressResults->Results->FindResult[0]->FoundLocation->Entity->DisplayName."</td>";
		echo "<td>".$findAddressResults->Results->FindResult[0]->FoundLocation->LatLong->Latitude."</td>";
		echo "<td>".$findAddressResults->Results->FindResult[0]->FoundLocation->LatLong->Longitude."</td></tr>";
		echo "</table>";
	} catch (SoapFault $e) {
		die($e->faultstring);
	}
}
elseif (isset($_GET['method'])) {
	showMethodUI($_GET['method']);
}
else {
?>
<table border="1" width="100%">
<tr class="header">
	<td><a href="javascript: void(0);" target="_self" onclick="fillMethods(0, document.getElementById('method')); return true;">Common Service</a></td>
	<td><a href="javascript: void(0);" target="_self" onclick="fillMethods(1, document.getElementById('method')); return true;">Find Service</a></td>
	<td><a href="javascript: void(0);" target="_self" onclick="fillMethods(2, document.getElementById('method')); return true;">Render Service</a></td>
	<td><a href="javascript: void(0);" target="_self" onclick="fillMethods(3, document.getElementById('method')); return true;">Route Service</a></td>
</tr>
<tr>
	<td colspan="4">
		<select name="method" id="method" onchange="window.open('<?php echo $_SERVER['PHP_SELF']; ?>?method=' + this.value, 'content');">
		</select>
	</td>
</tr>
<tr>
	<td colspan="4" height="520px">
	<iframe name="content" id="content" src="about:blank" frameborder="0" width="100%" height="100%">
	</iframe>
	</td>
</tr>
</table>
</body>
</html>
<?php
}
/* Functions */

function showMethodUI($method) {
	switch ($method) {

		case -1:
		return;

		//GetCountryRegionInfo
		case 0:
		?>
		<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" onsubmit="setValue(getOptionValues(this.entity.options), this.entities, 's'); return true;">
		<table border="1">
			<tr>
				<td>Entity ID: </td>
				<td><input type="text" name="entityID" id="entityID" value="" width="40px" /></td>
				<td><input type="button" name="addEntity" id="addEntity" value="Add" onclick="ent = document.getElementById('entityID').value; addOption(ent, ent, document.getElementById('entity'));" /></td>
				<td><input type="button" name="addEntity" id="remEntity" value="Rem" onclick="removeOption(document.getElementById('entity'))" /></td>
			</tr>
			<tr>
				<td></td>
				<td colspan="4">
				<select name="entity" id="entity" multiple size="5" style="width:100%">
					<option value="193">193</option>
					<option value='244'>244</option>
				</select>
				</td>
			</tr>
			<td>
				<input type="hidden" value="" name="entities" id="entities" />
				<td colspan="4" align="center"><input type="submit" id="GetCountryRegionInfo" name="GetCountryRegionInfo" value="Get Country Region Info" /></td>
			</tr>
		</table>
		</form>
		<?php
		break;

		case 1:
		//GetDataSourceInfo
		?>
		<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" onsubmit="setValue(getOptionValues(this.dataSource.options), this.dataSources, 's'); return true;">
		<table border="1">
			<tr>
				<td>Data source : </td>
				<td><input type="text" name="dataSourceName" id="dataSourceName" value="" width="40px" /></td>
				<td><input type="button" name="addDataSource" id="addDataSource" value="Add" onclick="ds = document.getElementById('dataSource').value; addOption(ent, ent, document.getElementById('dataSource'));" /></td>
				<td><input type="button" name="remDataSource" id="remDataSource" value="Rem" onclick="removeOption(document.getElementById('dataSource'))" /></td>
			</tr>
			<tr>
				<td></td>
				<td colspan="4">
				<select name="dataSource" id="dataSource" multiple size="5" style="width:100%">
					<option value="MapPoint.EU">MapPoint.EU</option>
					<option value="MapPoint.NA">MapPoint.NA</option>
				</select>
				</td>
			</tr>
			<td>
				<input type="hidden" value="" name="dataSources" id="dataSources" />
				<td colspan="4" align="center"><input type="submit" id="GetCountryRegionInfo" name="GetDataSourceInfo" value="Get Data Source Info" /></td>
			</tr>
		</table>
		</form>
		<?php
		break;

		case 2:
		//GetEntityTypes
		?>
			<br />
			<a href="samples/find.php" target="_self">samples/find.php</a>
		<?php
		break;

		case 3:
		//GetGreatCircleDistances
		?>
		<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
		<table border="1">
			<tr class="header">
				<td colspan="2">Get Great Circle Distances</td>
			</tr>
			<tr class="header">
				<td>Point no. 1</td>
				<td>Point no. 2</td>
			</tr>
			<tr>
				<td>Latitude: <input type="text" name="lat1" id="lat1" value="47.59975" /><br />
				Longitude:<input type="text" name="long1" id="long1" value="-122.33456" /></td>
				<td>Latitude: <input type="text" name="lat2" id="lat2" value="47.58306" /><br />
				Longitude:<input type="text" name="long2" id="long2" value="-122.23064" /></td>
			</tr>
			<tr>
				<td colspan="2" align="center">
				<input type="submit" name="GetGreatCircleDistances" id="GetGreatCircleDistances" value="Get Great Circle Distances" />
				</td>
			</tr>
		</table>
		</form>
		<?php
		break;

		case 4:
		//GetVersionInfo
		try {
			$vi = $_SESSION['mappoint']->CommonService->GetVersionInfo();
		} catch (SoapFault $e) {
			die($e->faultstring);
		}
		?>
		<table border="1">
			<tr class="header">
				<td colspan="2">Version Info</td>
			</tr>
			<tr class="header">
				<td>Component</td>
				<td>Version</td>
			</tr>
			<tr>
			<?php
			echo "<td>".$vi->Component."</td>";
			echo "<td>".$vi->Version."</td>";
			?>
		</tr>
		</table>
		<?php
		break;

		case 5:
		//Find
		?>
			<br />
			<a href="samples/find.php" target="_self">samples/find.php</a>
		<?php
		break;

		case 6:
		//FindAddress
		?>
		<br />
		<a href="samples/findAddress.php" target="_self">samples/findAddress.php</a>
		<br />
		<a href="samples/findNearby.php" target="_self">samples/findNearby.php</a>
		<br />
		<a href="samples/findNearRoute.php" target="_self">samples/findNearRoute.php</a>
		<?php
		break;

		case 7:
		//FindByID
		?>
		<br />
		<a href="samples/find.php" target="_self">samples/find.php</a>
		<?php
		break;

		case 8:
		//FindByProperty
		?>
		<br />
		<a href="samples/find.php" target="_self">samples/find.php</a>
		<?php
		break;

		case 9:
		//FindNearby
		?>
		<br />
		<a href="samples/findNearby.php" target="_self">samples/findNearby.php</a>
		<?php
		break;

		case 10:
		//FindNearRoute
		?>
		<br />
		<a href="samples/findNearRoute.php" target="_self">samples/findNearRoute.php</a>
		<?php
		break;

		case 11:
		//GetLocationInfo
		?>
		<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
		<table border="1">
		<tr class="header">
			<td>Location Info</td>
		</tr>
		<tr>
			<td>DataSource: 
			<select name="dataSource" id="dataSource">
				<option value="MapPoint.EU">MapPoint.EU</option>
				<option value="MapPoint.NA" selected="selected">MapPoint.NA</option>
			</select>
			</td>
		</tr>
		<tr>
			<td>Latitude:
			<input type="text" name="lat" id="lat" value="47.682" />
			</td>
		</tr>
		</tr>
			<td>Longitude:
			<input type="text" name="long" id="long" value="-122.132" />
			</td>
		</tr>
		<tr>
			<td align="center"><input type="submit" name="GetLocationInfo" id="GetLocationInfo" value="Get Location Info" /></td>
		</tr>
		</table>
		</form>
		<?php
		break;

		case 12:
		//ParseAddress
		?>
		<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
		<table border="1">
		<tr class="header">
			<td colspan="2">Parse Address</td>
		</tr>
		<tr>
			<td>Address:</td>
			<td><input type="text" name="address" id="address" value="1 Microsoft Way, Redmond" /></td>
		</tr>
		<tr>
			<td>Country:</td>
			<td><input type="text" name="country" id="country" value="United States" /></td>			
		</tr>
		<tr>
			<td>Data source:</td>
			<td>
				<select name="dataSource" id="dataSource">
					<option value="MapPoint.EU">MapPoint.EU</option>
					<option value="MapPoint.NA" selected="selected">MapPoint.NA</option>
				</select>
				</td>
		</tr>
		<tr>
			<td colspan="2" align="center"><input type="submit" name="ParseAddress" id="ParseAddress" value="Get Address" /></td>
		</tr>
		</table>
		</form>
		<?php
		break;
		
		case 13:
		//FindPolygon
		echo "<br />Not Tested.";
		break;

		
		case 14:
		//ConvertToLatLong
		?>
		<br />
		<a href="samples/panMapNavigation.php" target="_self">samples/panMapNavigation.php</a> <br />
		<?php
		break;

		case 15:
		//ConvertToPoint
		$myLatLongs = array();
		$myLatLongs[] = new LatLong();
		$myLatLongs[0]->Latitude = 47.59;
		$myLatLongs[0]->Longitude = -122.34;

		$myView  = new ViewByScale();
		$myView->CenterPoint = $myLatLongs[0];
		$myView->MapScale = 200000;
		
		try {
		$pixelCoords = $_SESSION['mappoint']->RenderService->ConvertToPoint($myLatLongs, $myView, 250, 250);
		} catch (SoapFault $e) {
			die($e-faultstring);
		}
		
		echo "<b>Latitude:</b> 47.59<br />";
		echo "<b>Longitude:</b> -122.34<br />";
		echo "<b>MapScale</b>: 1/200000<br />";
		echo "<b>View size</b>: 250px * 250px<br /><br />";
		echo "<b>Pixel coordinate of center point:</b> (".$pixelCoords[0]->X.", ".$pixelCoords[0]->Y.")";
		break;

		case 16:
		//GetBestMapView
		$myLocations = array();
		$myLocations[] = new Location();
		$myLocations[] = new Location();
		$myLocations[0]->LatLong = new LatLong();
		$myLocations[1]->LatLong = new LatLong();
		$myLocations[0]->LatLong->Latitude = 40;
		$myLocations[0]->LatLong->Longitude = -120;
		$myLocations[1]->LatLong->Latitude = 41;
		$myLocations[1]->LatLong->Longitude = -121;
		try {
			$mapRepresentations = $_SESSION['mappoint']->RenderService->GetBestMapView($myLocations, "MapPoint.NA");
		} catch (SoapFault $e) {
			die($e->faultstring);
		}
		echo "<b>Point no. 1:</b> (40, -120)<br />";
		echo "<b>Point no. 2:</b> (41, -121)<br /><br />";
		echo "Output of MapViewRepresentations object: <br /><br />";
		echo "<div style='font-size: small'><b>Note:</b> SoapClient is uncapable of mapping classes descendant from an abstract class.
		Notice that ByScale, ByBoundingRectangle and ByHeightWidth are objects of stdClass. These are descendants of MapView, an abstract class.
		This means that if the web service response sends objects like these you cannot use them in other methods of the service.
		You <b>MUST</b> create new instances of the Views with the same property values and replace the stdClass objects by the new ones created.
		Otherwise, you might end up with a PHP error with message 'undefined method: stdClass::transform()'.
		</div><br /><br />";
		print_r($mapRepresentations);
		break;

		case 17:
		//GetMap
		?>
		<br />
		<a href="samples/findAddress.php" target="_self">samples/findAddress.php</a> (using a ViewByScale object) <br />
		<a href="samples/route.php" target="_self">samples/route.php</a> (using a ViewByBoundingRectangle object) <br />
		<a href="samples/findNearby.php" target="_self">samples/findNearby.php</a> (using a ViewByBoundingLocations object)<br />
		<a href="samples/panMapNavigation.php" target="_self">samples/panMapNavigation.php</a> (using a ViewByHeightWidth object)<br />
		<a href="samples/findNearRoute.php" target="_self">samples/findNearRoute.php</a> <br />		
		<?php
		break;
		
		case 18:
		//GetLineDriveMap
		?>
		<br />
		<a href="samples/lineDrive.php" target="_self">samples/lineDrive.php</a> <br />
		<?php
		break;

		case 19:
		//CalculateRoute
		?>
		<br />
		<a href="samples/findNearRoute.php" target="_self">samples/findNearRoute.php</a>
		<?php
		break;

		case 20:
		//CalculateSimpleRoute
		?>
		<br />
		<a href="samples/route.php" target="_self">samples/route.php</a> <br />
		<?php
		break;

		default:
		die('Unexpected error!');
	}
}
?>