Login   Register  
PHP Classes
elePHPant
Icontem

File: samples/find.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/find.php  >  Download  
File: samples/find.php
Role: Example script
Content type: text/plain
Description: Find test
Class: MapPoint PHP API
Access MapPoint Web services
Author: By
Last change:
Date: 8 years ago
Size: 7,724 bytes
 

Contents

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


/**
 * Populates a select element based on an array of FindResults objects.
 *
 * @param array $myResults an array of FindResults objects
 */
function populateListBox($myResults) {
    echo 
"<select name='listboxFindNearbyResults' size='4' id='listboxFindNearbyResults' style='width:320px'>";
    foreach (
$myResults as $result) {
        echo 
"<option value='".$result->FoundLocation->LatLong->Latitude."/"
        
.$result->FoundLocation->LatLong->Longitude."'>";
        if (
$result->FoundLocation->Entity->DisplayName == null) {
            echo 
$result->FoundLocation->Entity->Name;
        } else {
            echo 
$result->FoundLocation->Entity->DisplayName;
        }
        echo 
"</option>";
    }
    echo 
"</select>";
}


if (!isset(
$_SESSION['props'])) {
    
$_SESSION['props'] = array();
}
if (isset(
$_POST['getpropnames'])) {
    
$global $_SESSION['mappoint'];

    
$ets null;
    try {
        
$ets $global->CommonService->GetEntityTypes("MapPoint.FourthCoffeeSample");
        
$_SESSION['props'] = array();
        foreach(
$ets as $et) {
            if(
strcmp($et->Name"FourthCoffeeShops") == 0) {
                foreach(
$et->Properties->Property as $ep) {
                    
$_SESSION['props'][] = $ep->Name;
                }
                break;
            }
        }
    }
    catch (
SoapFault $e){
        die(
$e->faultstring);
    }
}
?>
<!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>Find</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<table border="1">
    <tr>
        <td colspan="2"><b>Sample Find</b></td>
    </tr>
    <tr>
        <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
        <td colspan="2"><b>Find a place</b></td>
    </tr>
    <tr>
        <td>Data source:</td>
        <td>
        <select name="listDataSourceFind" id="listDataSourceFind">
            <option selected="selected" value="MapPoint.NA">MapPoint.NA</option>
            <option value="MapPoint.EU">MapPoint.EU</option>
        </select>
        </td>
    </tr>
    <tr>
        <td>Place name:</td>
        <td><input name="textFindString" type="text" value="Seattle" id="textFindString" /></td>
    </tr>
    <tr>
        <td>Geographic context:</td>
        <td>
        <select name="listEntityType" id="listEntityType">
            <option value="-1"></option>
            <option value="PopulatedPlace">Cities</option>
            <option value="AdminDivision1">State/Province</option>
        </select>
        </td>
    </tr>
    <tr>
        <td align="center" colspan="2"><input type="submit" name="buttonFind" value="Find place" id="buttonFind" /></td>
    </tr>
    <tr>
        <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
        <TD colspan="2">&nbsp;</TD>
    </tr>
    <tr>
        <td colspan="2"><b>Find an entity by ID</b></td>
    </tr>
    <tr>
        <td>Data source:</td>
        <td><input name="datasourcename" type="text" value="MapPoint.FourthCoffeeSample" id="datasourcename" /></td>
    </tr>
    <tr>
        <td>Entity ID:</td>
        <td><input name="entityID" type="text" value="-1191" id="entityID" /></td>
    </tr>
    <tr>
        <td align="center" colspan="2"><input type="submit" name="findByID" value="Find Entity by ID" id="findByID" /></td>
    </tr>
    <tr>
        <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
        <td colspan="2"><b>Find an entity by property</b></td>
    </tr>
    <tr>
        <td>Data source:</td>
        <td>MapPoint.FourthCoffeeSample</td>
    </tr>
    <tr>
        <td>Property Name:</td>
        <td>
        <select name="propertylist" id="propertylist">
        <?php
        
foreach ($_SESSION['props'] as $prop) {
            echo 
"<option value='$prop'>$prop</option>";
        }
        
?>
        </select>
        <input type="submit" name="getpropnames" value="Get Names" id="getpropnames" />
        </td>
    </tr>
    <tr>
        <td>Property Value:</td>
        <td><input name="propertyValue" type="text" id="propertyValue" /></td>
    </tr>
    <tr>
        <td align="center" colspan="2">
        <input type="submit" name="findbyprop" value="Find Entities by Properties" id="findbyprop" /></td>
    </tr>
    <tr>
        <td colspan="2">&nbsp;</td>
    </tr>
</table>


<?php

if (isset($_POST['buttonFind'])) {
    
$global $_SESSION['mappoint'];
    
//Set up the specification object
    
$findSpec = new FindSpecification();
    
$findSpec->InputPlace $_POST['textFindString'];
    
$findSpec->DataSourceName $_POST['listDataSourceFind'];

    
//Set up the Find options to allow more return values (by decreasing the threshold)
    //but also limit the number of results returned to 20
    
$myFindOptions = new FindOptions();
    
$myFindOptions->ThresholdScore 0.5;
    
$myFindOptions->Range = new FindRange();
    
$myFindOptions->Range->StartIndex 0;
    
$myFindOptions->Range->Count 20;
    
$findSpec->Options $myFindOptions;

    
//If an entity type is selected, then limit the results to only those entity types.
    
$myEntityTypes = array();
    if (
$_POST['listEntityType'] != -1) {
        
$myEntityTypes[]= $_POST['listEntityType'];
        
$findSpec->EntityTypeNames $myEntityTypes;
    }


    
// Create a FindResults object to store the results of the Find request
    
$myFindResults null;
    try {
        
$myFindResults $global->FindService->Find($findSpec);
        if (
$myFindResults->Results != null) {
            
// Populate the list box with the returned results.
            
populateListBox($myFindResults->Results->FindResult);
        }
    }
    catch (
SoapFault $e) {
        die(
$e->faultstring);
    }
} elseif (isset(
$_POST['findByID'])) {
    
$global $_SESSION['mappoint'];
    
$findSpec = new FindByIDSpecification();
    
$findSpec->DataSourceName $_POST['datasourcename'];
    
$findSpec->EntityIDs = array($_POST['entityID']);

    
//Create a FindFilter to define the entity type
    
$findSpec->Filter = new FindFilter();
    
$findSpec->Filter->EntityTypeName "FourthCoffeeShops";
    
$findSpec->Filter->PropertyNames = array("DisplayName");

    
// Create a FindResults object to store the results of the Find request
    
$myFindResults null;
    try {
        
$myFindResults $global->FindService->FindByID($findSpec);

        if (
$myFindResults->Results != null) {
            
// Populate the list box with the returned results.
            
populateListBox($myFindResults->Results->FindResult);
        }
    }
    catch (
SoapFault $e) {
        die(
$e->faultstring);
    }
} elseif (isset(
$_POST['findbyprop'])) {
    
$global $_SESSION['mappoint'];
    try {
        
$propertyName '';
        if(
$_POST['propertylist'] != null) {
            
$propertyName .= $_POST['propertylist'];
        }

        
//Set up the specification object
        
$findSpec = new FindByPropertySpecification();
        
$findSpec->DataSourceName "MapPoint.FourthCoffeeSample";

        
//Create a FindFilter to define filter Expression
        
$findSpec->Filter = new FindFilter();
        
$findSpec->Filter->EntityTypeName "FourthCoffeeShops";
        
$findSpec->Filter->WhereClause = new WhereClause();
        
$findSpec->Filter->PropertyNames = array("DisplayName");


        
//Create a where clause
        
$findSpec->Filter->WhereClause->SearchProperties = array();
        
$findSpec->Filter->WhereClause->SearchProperties[] = new EntityPropertyValue();
        
$findSpec->Filter->WhereClause->SearchProperties[0]->Name $propertyName;
        
$findSpec->Filter->WhereClause->SearchProperties[0]->Value $_POST['propertyValue'];


        
// Create a FindResults object to store the results of the Find request
        
$myFindResults null;

        
$myFindResults $global->FindService->FindByProperty($findSpec);

        if (
$myFindResults->Results != null) {
            
// Populate the list box with the returned results.
            
populateListBox($myFindResults->Results->FindResult);
        }
    }
    catch (
SoapFault $e) {
        
/*
        FindByProperty method always throws this exception:
        'The type of the specified property does not match the type of the compared value 
        or input string was not in a correct format. 
        Argument: specification.Filter.WhereClause.SearchProperties[0].Name'
        I also tested this on asp .net with a sample that comes with the Mappoint SDK and a SoapException
        with this message is also thrown.
        */
        
die($e->faultstring);
    }
}
?>

</select>
</form>
</body>
</html>