PHP Classes

File: samples/route.php

Recommend this page to a friend!
  Packages of Carlos Machado   MapPoint PHP API   samples/route.php   Download  
File: samples/route.php
Role: Example script
Content type: text/plain
Description: CalculateSimpleRoute test
Class: MapPoint PHP API
Access MapPoint Web services
Author: By
Last change:
Date: 21 years ago
Size: 8,027 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>CalculateSimpleRoute</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<table border="1">
<tr>
    <td colspan="2"><b>Route with Map and Directions</b></td>
</tr>
<tr>
    <td>Data source: </td>
    <td>
    <select name="dataSource" id="dataSource">
        <option value="MapPoint.EU">MapPoint.EU</option>
        <option value="MapPoint.NA">MapPoint.NA</option>
        <option value="MapPoint.BR">MapPoint.BR</option>
    </select>
    </td>
</tr>
<tr>
    <td colspan="2"><b>Start Address</b></td>
</tr>
<tr>
    <td>Street Address:</td>
    <td><input type="text" name="startAddress" id="startAddress" /></td>
</tr>
<tr>
    <td>City:</td>
    <td><input type="text" name="startCity" id="startCity" /></td>
</tr>
<tr>
    <td>Secondary city:</td>
    <td><input type="text" name="startSCity" id="startSCity" /></td>
</tr>
<tr>
    <td>Subdivision:</td>
    <td><input type="text" name="startSubdivision" id="startSubdivision" /></td>
</tr>
<tr>
    <td>Postal code:</td>
    <td><input type="text" name="startPostalCode" id="startPostalCode" /></td>
</tr>
<tr>
    <td>Country or region:</td>
    <td><input type="text" name="startCountry" id="startCountry" /> </td>
</tr>
<tr>
    <td colspan="2"><b>End Address</b></td>
</tr>
<tr>
    <td>Street Address:</td>
    <td><input type="text" name="endAddress" id="endAddress" /></td>
</tr>
<tr>
    <td>City:</td>
    <td><input type="text" name="endCity" id="endCity" /></td>
</tr>
<tr>
    <td>Secondary city:</td>
    <td><input type="text" name="endSCity" id="endSCity" /></td>
</tr>
<tr>
    <td>Subdivision:</td>
    <td><input type="text" name="endSubdivision" id="endSubdivision" /></td>
</tr>
<tr>
    <td>Postal code:</td>
    <td><input type="text" name="endPostalCode" id="endPostalCode" /></td>
</tr>
<tr>
    <td>Country or region:</td>
    <td><input type="text" name="endCountry" id="endCountry" /> </td>
</tr>
<tr>
    <td><b>Choose a language:</b></td>
    <td>
    <select name="language" id="language">
    <option value="nl">Dutch</option>
    <option value="en">English</option>
    <option value="en-US">English-USA</option>
    <option value="fr">French</option>
    <option value="de">German</option>
    <option value="it">Italian</option>
    <option value="js">Japanese</option>
    <option value="pt" selected="selected">Portuguese :)</option> <!-- VIVA PORTUGAL !!! -->
    <option value="es">Spanish</option>
    <option value="sv">Swedish</option>
    </select>
    </td>
</tr>
<tr>
    <td><b>Segment preference:</b></td>
    <td>
    <input type="radio" name="segment" id="quick" value="<?php echo SegmentPreference::$Quickest; ?>" checked="checked" />Quickest&nbsp;&nbsp;
    <input type="radio" name="segment" id="short" value="<?php echo SegmentPreference::$Shortest; ?>" />Shortest
    </td>
</tr>
<tr>
    <td colspan="2" style="text-align:center"><input type="submit" name="getRoute" id="getRoute" value="Get route"></td>
</tr>
</table>
</form>
<?php

if (isset($_POST['getRoute'])) {
    /*This code is a translation of the Route sample of the Mappoint Documention.
    I'd recommend you to check the sample and compare it with this one.*/
    $global = $_SESSION['mappoint'];
    $myAddressPointA = new Address();
    $myAddressPointA->AddressLine = $_POST['startAddress'];
    $myAddressPointA->PrimaryCity = $_POST['startCity'];
    $myAddressPointA->SecondaryCity = $_POST['startSCity'];
    $myAddressPointA->Subdivision = $_POST['startSubdivision'];
    $myAddressPointA->PostalCode = $_POST['startPostalCode'];
    $myAddressPointA->CountryRegion = strtoupper($_POST['startCountry']);

    $findAddressSpecA = new FindAddressSpecification();
    $findAddressSpecA->InputAddress = $myAddressPointA;
    $findAddressSpecA->DataSourceName = $_POST['dataSource'];

    $global->FindService->UserInfoHeaderValue = new UserInfoHeader();
    $cult = new CultureInfo();
    $cult->Lcid = $_POST['language'];
    $cult->Name = 'pt';
    $global->FindService->UserInfoHeaderValue->Culture = $cult;
    $myFindResultsPointA = null;
    try {
        $myFindResultsPointA = $global->FindService->FindAddress($findAddressSpecA);
    } catch (SoapFault $e) {
        die($e->faultstring);
    }

    $myAddressPointB = new Address();
    $myAddressPointB->AddressLine = $_POST['endAddress'];
    $myAddressPointB->PrimaryCity = $_POST['endCity'];
    $myAddressPointB->SecondaryCity = $_POST['endSCity'];
    $myAddressPointB->Subdivision = $_POST['endSubdivision'];
    $myAddressPointB->PostalCode = $_POST['endPostalCode'];
    $myAddressPointB->CountryRegion = strtoupper($_POST['endCountry']);

    $findAddressSpecB = new FindAddressSpecification();
    $findAddressSpecB->InputAddress = $myAddressPointB;
    $findAddressSpecB->DataSourceName = $_POST['dataSource'];

    $myFindResultsPointB = null;
    try {
        $myFindResultsPointB = $global->FindService->FindAddress($findAddressSpecB);
    } catch (SoapFault $e) {
        die($e->faultstring);
    }
    if ($myFindResultsPointA->NumberFound == 0 || $myFindResultsPointB->NumberFound == 0) {
        echo "<br />No results found.";
    } else {
        $myWaypoints = array();
        //NOTE: FindResult can be either an array or an object.
        //This code might produce errors because of that.
        $myWaypoints[] = $myFindResultsPointA->Results->FindResult->FoundLocation->LatLong;
        $myWaypoints[] = $myFindResultsPointB->Results->FindResult->FoundLocation->LatLong;

        $myRoute = null;
        try {
            //set locale
            $global->RouteService->UserInfoHeaderValue = new UserInfoHeader();
            $cult = new CultureInfo();
            $cult->Name = $_POST['language'];
            $global->RouteService->UserInfoHeaderValue->Culture = $cult;
            
            $myRoute = $global->RouteService->CalculateSimpleRoute($myWaypoints, $_POST['dataSource'], $_POST['segment']);
            echo "<br /><select name='direction' id='direction' multiple='multiple' size='10' style='width:320px'>";
            foreach ($myRoute->Itinerary->Segments->Segment as $segment) {
                foreach ($segment->Directions->Direction as $direction) {
                    $strItemText = '';
                    if ($direction != null && $direction instanceof Direction) {
                        $strItemText .= $direction->Instruction . " ";
                        $strItemText .= "(Distance: " . round($direction->Distance, 2) . " km; ";
                        $strItemText .= "Duration: " . round($direction->Duration/60, 2) . " minutes)";
                    }
                    echo "<option value='$strItemText'>$strItemText</option>";
                }
            }
            echo "</select><br />";
            $myPushPins = array();
            $pp1 = new Pushpin();
            $pp1->PinID = "pin0";
            $pp1->Label = "Start";
            $pp1->IconName = "31";
            $pp1->IconDataSource = "MapPoint.Icons";
            $pp1->LatLong = $myWaypoints[0];
            $myPushPins[] = $pp1;

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

            $myMapOptions = new MapOptions();
            $myMapOptions->ReturnType = MapReturnType::$ReturnUrl;
            $myMapOptions->Format = new ImageFormat();
            $myMapOptions->Format->Height = 500;
            $myMapOptions->Format->Width = 500;

            //a new instance of the view MUST be created, because SoapClient uncapable of mapping the MapView class descendants
            $myMapView = new ViewByBoundingRectangle();
            $myMapView->BoundingRectangle->Southwest = $myRoute->Itinerary->View->ByBoundingRectangle->BoundingRectangle->Southwest;
            $myMapView->BoundingRectangle->Northeast = $myRoute->Itinerary->View->ByBoundingRectangle->BoundingRectangle->Northeast;
            //$myMapView = $myRoute->Itinerary->View->ByBoundingRectangle;

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

            $mapImage = $global->RenderService->GetMap($mapSpec);


            echo "<img src='$mapImage->Url' />";
        } catch (SoapFault $e) {
            die($e->faultstring);
        }
    }
}
?>
</body>
</html>