To run the following example, view a map created with the sample data. Using one of the selection tools, select one or more parcels.
The example creates a Resource Service connection to open the map and a Feature Service connection to read the feature information. In addition, you will require an MgFeatureQueryOptions object for creating the list of features.
The MgSelection object contains selection information for a given map. It can either contain the current selection or a selection created from XML data. For this example, create the object with the current map selection.
Get the list of layers and iterate through them. When the $layers collection is empty, there are no features selected.
//
Initialize the Web Extensions and connect to the Server using
//
the Web Extensions session identifier stored in PHP
//
session state.
MgInitializeWebTier ($configFilePath);
$userInfo = new MgUserInformation($mgSessionId);
$siteConnection = new MgSiteConnection();
$siteConnection->Open($userInfo);
$resourceService = $siteConnection->CreateService(
MgServiceType::ResourceService);
$featureService = $siteConnection->CreateService(
MgServiceType::FeatureService);
$queryOptions = new MgFeatureQueryOptions();
$map = new MgMap();
$map->Open($resourceService, $mapName);
//
----- Beginning of AJAX-specific code ----------
//
Create the selection object by retrieving the current
//
selection from the map.
$selection = new MgSelection($map);
$selection->Open($resourceService, $mapName);
//
----- End of AJAX-specific code ----------------
$layers = $selection->GetLayers();
if ($layers)
{
for ($i = 0; $i < $layers->GetCount(); $i++)
{
//
Only check selected features in the Parcels layer.
$layer = $layers->GetItem($i);
if ($layer && $layer->GetName() == 'Parcels')
{
//
Create a filter containing the IDs of the selected
//
features on this layer
$layerClassName = $layer->GetFeatureClassName();
$selectionString = $selection->GenerateFilter(
$layer, $layerClassName);
//
Get the feature resource for the selected layer
$layerFeatureId = $layer->GetFeatureSourceId();
$layerFeatureResource = new
MgResourceIdentifier($layerFeatureId);
//
Apply the filter to the feature resource for the
//
selected layer. This returns
//
an MgFeatureReader of all the selected features.
$queryOptions->SetFilter($selectionString);
$featureReader = $featureService->SelectFeatures(
$layerFeatureResource, $layerClassName, $queryOptions);
//
Process each item in the MgFeatureReader, displaying the
//
owner name and address
while ($featureReader->ReadNext())
{
$val = $featureReader->GetString('NAME') . ', ' .
$featureReader->GetString('RPROPAD');
echo $val . '<br />';
}
}
}
}
else
echo 'No selected layers';