Login   Register  
PHP Classes
elePHPant
Icontem

File: samples/findNearRoute.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  >  samples/findNearRoute.php  >  Download  
File: samples/findNearRoute.php
Role: Example script
Content type: text/plain
Description: FindNearRoute test
Class: MapPoint PHP API
Access MapPoint Web services
Author: By
Last change:
Date: 2005-10-19 09:13
Size: 11,597 bytes
 

Contents

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

?>
<!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>Route with Map and Directions</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table border="1">
<tr>
	<td align="center" colspan="2"><b>Sample FindNearRoute</b></td>
</tr>
<tr>
	<td colspan="2">Data source:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<select name="listDataSource" id="listDataSource">
		<option selected="selected" value="MapPoint.NA">MapPoint.NA</option>
		<option value="MapPoint.EU">MapPoint.EU</option>
	</select>
	</td>
</tr>
<tr>
	<td colspan="2">&nbsp;</td>
</tr>
<tr>
	<td>
	<table>
	<tr>
		<td colSpan="2"></b>Start address</b></td>
	</tr>
	<tr>
		<td>Street address:</td>
		<td><input name="textAddressLineA" type="text" value="1 Microsoft Way" id="textAddressLineA" /></td>
	</tr>
	<tr>
		<td>City:</td>
		<td><input name="textPrimaryCityA" type="text" value="Redmond" id="textPrimaryCityA" /></td>
	</tr>
	<tr>
		<td>Secondary city:</td>
		<td><input name="textSecondaryCityA" type="text" id="textSecondaryCityA" /></td>
	</tr>
	<tr>
		<td>Subdivision:</td>
		<td><input name="textSubdivisionA" type="text" value="WA" id="textSubdivisionA" /></td>
	</tr>
	<tr>
		<td>Postal code:</td>
		<td><input name="textPostalCodeA" type="text" value="98052" id="textPostalCodeA" /></td>
	</tr>
	<tr>
		<td>Country or region:</td>
		<td><input name="listCountryRegionA" type="text" value="USA" id="listCountryRegionA" /></td>
	</tr>
	</table>
	</td>
	<td>
	<table>
	<tr>
		<td colSpan="2"><b>End address:</b></td>
	</tr>
	<tr>
		<td>Street address:</td>
		<td><input name="textAddressLineB" type="text" value="2211 Elliott Avenue" id="textAddressLineB" /></td>
	</tr>
	<tr>
		<td>City:</td>
		<td><input name="textPrimaryCityB" type="text" value="Seattle" id="textPrimaryCityB" /></td>
	</tr>
	<tr>
		<td>Secondary city:</td>
		<td><input name="textSecondaryCityB" type="text" id="textSecondaryCityB" /></td>
	</tr>
	<tr>
		<td>Subdivision:</td>
		<td><input name="textSubdivisionB" type="text" value="WA" id="textSubdivisionB" /></td>
	</tr>
	<tr>
		<td>Postal code:</td>
		<td><input name="textPostalCodeB" type="text" value="98121" id="textPostalCodeB" /></td>
	</tr>
	<tr>
		<td>Country or region:</td>
		<td><input name="listCountryRegionB" type="text" value="USA" id="listCountryRegionB" /></td>
	</tr>
	</table>
	</td>
	</tr>
	<tr>
		<td align="left" colspan="2">All coffee shops within
		<select name="distance" id="distance">
			<option value="0.5">0.5</option>
			<option selected="selected" value="1.0">1.0</option>
			<option value="2.0">2.0</option>
		</select>miles of the route
		</td>
	</tr>
	<tr>
		<td colspan="2">&nbsp;</td>
	</tr>
	<tr>
		<td align="center" colspan="2"><input type="submit" name="buttonGetRoute" value="Get Fourth Coffee Shops near the route" id="buttonGetRoute" /></td>
	</tr>
	<tr>
		<td colspan="2">&nbsp;</td>
	</tr>
	<tr>
		<td align="center" colspan="2"></td>
	</tr>
	<tr>
		<td align="center" colspan="2"></td>
	</tr>
	<tr>
		<td align="center" colspan="2"></td>
	</tr>
	<tr>
		<td align="center" colspan="2"></td>
	</tr>
	<tr>
		<td align="center" colspan="2"></td>
	</tr>
</table>
</form>
<?php

if (isset($_POST['buttonGetRoute'])) {
	$global = $_SESSION['mappoint'];

	//Create the address object for the start point
	$myAddressPointA = new Address();
	$myAddressPointA->AddressLine = $_POST['textAddressLineA'];
	$myAddressPointA->PrimaryCity = $_POST['textPrimaryCityA'];
	$myAddressPointA->SecondaryCity = $_POST['textSecondaryCityA'];
	$myAddressPointA->Subdivision = $_POST['textSubdivisionA'];
	$myAddressPointA->PostalCode = $_POST['textPostalCodeA'];
	$myAddressPointA->CountryRegion = $_POST['listCountryRegionA'];

	//Set up the specification object
	$findAddressSpecA = new FindAddressSpecification();
	$findAddressSpecA->InputAddress = $myAddressPointA;
	$findAddressSpecA->DataSourceName = $_POST['listDataSource'];

	//Find the first address
	$myFindResultsPointA = null;

	try {
		$myFindResultsPointA = $global->FindService->FindAddress($findAddressSpecA);

	} catch (SoapFault $e) {
		die($e->faultstring);
	}


	//Create the address object for the end point
	$myAddressPointB  = new Address();
	$myAddressPointB->AddressLine = $_POST['textAddressLineB'];
	$myAddressPointB->PrimaryCity = $_POST['textPrimaryCityB'];
	$myAddressPointB->SecondaryCity = $_POST['textSecondaryCityB'];
	$myAddressPointB->Subdivision = $_POST['textSubdivisionB'];
	$myAddressPointB->PostalCode = $_POST['textPostalCodeB'];
	$myAddressPointB->CountryRegion = $_POST['listCountryRegionB'];

	//Set up the specification object
	$findAddressSpecB = new FindAddressSpecification();
	$findAddressSpecB->DataSourceName = $_POST['listDataSource'];
	$findAddressSpecB->InputAddress = $myAddressPointB;

	//Find the second address
	$myFindResultsPointB = null;

	try {
		$myFindResultsPointB = $global->FindService->FindAddress($findAddressSpecB);

	} catch(SoapFault $e) {
		die($e->faultstring);
	}

	if ($myFindResultsPointA == null || ($myFindResultsPointA->NumberFound == 0) || ($myFindResultsPointB->NumberFound == 0) ){
		//If no results were found for either point, just tell the user this
		die("Start or end points were not found.");
	} else {
		//Route between these two addresses

		//Create the waypoint array to hold the two lat/long values
		$myWaypoints = array();
		$myWaypoints[] = $myFindResultsPointA->Results->FindResult[0]->FoundLocation->LatLong;
		$myWaypoints[] = $myFindResultsPointB->Results->FindResult[0]->FoundLocation->LatLong;

		//Set up the datasource to use
		$myDataSourceName = $_POST['listDataSource'];


		//Route between the two points
		$myRoute = null;

		try {
			$routespec = new RouteSpecification();
			$routespec->DataSourceName = $myDataSourceName;
			$routespec->Segments = array();
			$routespec->Segments[] = new SegmentSpecification();
			$routespec->Segments[0]->Waypoint = new Waypoint();
			$routespec->Segments[0]->Waypoint->Location = new Location();
			$routespec->Segments[0]->Waypoint->Location->LatLong = $myWaypoints[0];
			$routespec->Segments[0]->Waypoint->Name = "Start";

			$routespec->Segments[] = new SegmentSpecification();
			$routespec->Segments[1]->Waypoint = new Waypoint();
			$routespec->Segments[1]->Waypoint->Location = new Location();
			$routespec->Segments[1]->Waypoint->Location->LatLong = $myWaypoints[1];
			$routespec->Segments[1]->Waypoint->Name = "End";

			$myRoute = $global->RouteService->CalculateRoute($routespec);


			//Now get the fourth coffee shops near this route
			$fnrspec = new FindNearRouteSpecification();
			$fnrspec->DataSourceName = "MapPoint.FourthCoffeeSample";
			$fnrspec->Distance = $_POST['distance'];
			$fnrspec->Route = $myRoute;
			$fnrspec->Filter = new FindFilter();
			$fnrspec->Filter->EntityTypeName = "FourthCoffeeShops";

			$fnrs = $global->FindService->FindNearRoute($fnrspec);

			//Now get a map of the route, marking the start and end points with pushpins
			//Also mark all the coffee shops along the route
			$myPushPins = array();

			$myPushPins[] = new Pushpin();
			$myPushPins[0]->PinID = "pin0";
			$myPushPins[0]->Label = "Start";
			$myPushPins[0]->IconName = "31";
			$myPushPins[0]->IconDataSource = "MapPoint.Icons";
			$myPushPins[0]->LatLong = $myWaypoints[0];

			$myPushPins[] = new Pushpin();
			$myPushPins[1]->PinID = "pin1";
			$myPushPins[1]->Label = "End";
			$myPushPins[1]->IconName = "29";
			$myPushPins[1]->IconDataSource = "MapPoint.Icons";
			$myPushPins[1]->LatLong =  $myWaypoints[1];

			//array containing the found locations
			$csLocations = array();
			$csLocations[] = "<b>Coffee shop locations:</b>";
			$found = count($fnrs->Results->FindResult);
			for($i=2; $i < $found + 2; $i++) {
				$myPushPins[$i] = new Pushpin();
				$myPushPins[$i]->PinID = "pin" + $i;
				//myPushPins[i].Label = fnrs.Results[i-2].FoundLocation.Entity.DisplayName;
				$myPushPins[$i]->IconName = "RedCircle" + ($i-1);
				$myPushPins[$i]->IconDataSource = "MapPoint.Icons";
				$myPushPins[$i]->LatLong =  new LatLong();
				$myPushPins[$i]->LatLong->Latitude = $fnrs->Results->FindResult[$i-2]->FoundLocation->LatLong->Latitude;
				$myPushPins[$i]->LatLong->Longitude = $fnrs->Results->FindResult[$i-2]->FoundLocation->LatLong->Longitude;


				$addressString = $fnrs->Results->FindResult[$i-2]->FoundLocation->Address->AddressLine .
				", " + $fnrs->Results->FindResult[$i-2]->FoundLocation->Address->PrimaryCity .
				", " . $fnrs->Results->FindResult[$i-2]->FoundLocation->Address->Subdivision .
				" " . $fnrs->Results->FindResult[$i-2]->FoundLocation->Address->PostalCode;
				$csLocations[] = ($i-1) . ". " . $addressString;
			}

			//Set up the map options
			$myMapOptions = new MapOptions();
			$myMapOptions->Format = new ImageFormat();
			$myMapOptions->Format->Height = 500;
			$myMapOptions->Format->Width = 500;

			//Set up the specification object
			$myMapViews = array();
			
			//You can't do the line below. You have to create an instance of a View.
			//SoapClient doesn't understand well abstract types, so ByBoundingRectangle will
			//always be an instance of stdClass
			//$myMapViews[] = $myRoute->Itinerary->View->ByBoundingRectangle;
			
			$myMapViews[] = new ViewByBoundingRectangle();
			$myMapViews[0]->BoundingRectangle = $myRoute->Itinerary->View->ByBoundingRectangle->BoundingRectangle;

			$mapSpec = new MapSpecification();
			$mapSpec->Views = $myMapViews;
			$mapSpec->Options = $myMapOptions;
			$mapSpec->DataSourceName = $_POST['listDataSource'];
			$mapSpec->Pushpins = $myPushPins;
			$mapSpec->Route = $myRoute;

			$myMapImages = null;
			$myMapImages = $global->RenderService->GetMap($mapSpec);

			echo "<br /><img src='".$myMapImages[0]->Url."' />";

			//Print out the directions
			echo "<br /><select size='15' multiple='multiple' style='width:500px'>";
			//Iterate through each segment of the route
			foreach($myRoute->Itinerary->Segments->Segment as $routeSegment) {
				//Output each direction of the segment
				foreach($routeSegment->Directions->Direction as $key => $direction) {
					$strItemText = "";
					if ($direction != null and $direction instanceof Direction) {
						$strItemText .= $direction->ID . ". ";
						$strItemText .= $direction->Instruction . " ";
						$strItemText .= "(Distance: " . round($direction->Distance, 2) .  " kilometers; ";
						$strItemText .= "Duration: " . round($direction->Duration/60, 2) . " minutes)";
					}
					echo "<option value='$key'>$strItemText</option>";
				}
			}
			echo "</select>";
			//Output the summary of the route
			echo "<br /><b>Total Distance</b>: " . round($myRoute->Itinerary->Segments->Segment[0]->Distance,2) . " kilometers";
			echo "<br /><b>Total Trip Time</b>: " . round($myRoute->Itinerary->Segments->Segment[0]->TripTime/60, 2) . " minutes";
			
			echo "<br /><table border='1'>";
			foreach ($csLocations as $location) {
				echo "<tr><td>$location</td></tr>";
			}
			echo "</table>";
		} catch (SoapFault $e) {
			die($e->faultstring);
		}
	}
}
?>
</body>
</html>