Example: Setting the Active Selection

The following example combines the pieces needed to create a selection using the Web API and pass it back to the Viewer where it becomes the active selection for the map. It is an extension of the example shown in Example: Selection.

The PHP code in this example creates the selection XML. Following that is a JavaScript function that calls the SetSelectionXML() function with the selection. This function is executed when the page loads.

    <body class="AppFrame" onLoad="OnPageLoad()">

      <h1 class="AppHeading">Select features</h1>

      <?php

      include '../common/common.php';

      $args = ($_SERVER['REQUEST_METHOD'] == "POST")? $_POST : $_GET;

      $sessionId = $args['SESSION'];

      $mapName = $args['MAPNAME'];

      try

      {

        // Initialize the Web Extensions and connect to the Server

        // using the Web Extensions session identifier

        MgInitializeWebTier ($webconfigFilePath);

        $userInfo = new MgUserInformation($sessionId);

        $siteConnection = new MgSiteConnection();

        $siteConnection->Open($userInfo);

        $map = new MgMap($siteConnection);

        $map->Open($mapName);

        // Get the geometry for the boundaries of District 1

        $districtQuery = new MgFeatureQueryOptions();

        $districtQuery->SetFilter("Autogenerated_SDF_ID = 1");

        $layer = $map->GetLayers()->GetItem('Districts');

        $featureReader = $layer->SelectFeatures($districtQuery);

        $featureReader->ReadNext();

        $districtGeometryData = $featureReader->

        GetGeometry('Data');

        // Convert the AGF binary data to MgGeometry.

        $agfReaderWriter = new MgAgfReaderWriter();

        $districtGeometry =

          $agfReaderWriter->Read($districtGeometryData);

        // Create a filter to select the desired features. Combine

        // a basic filter and a spatial filter.

        $queryOptions = new MgFeatureQueryOptions();

        $queryOptions->SetFilter("RNAME LIKE 'SCHMITT%'");

        $queryOptions->SetSpatialFilter('SHPGEOM', $districtGeometry,

          MgFeatureSpatialOperations::Inside);

        // Get the features from the feature source,

        // turn it into a selection, then save the selection as XML.

        $layer = $map->GetLayers()->GetItem('Parcels');

        $featureReader = $layer->SelectFeatures($queryOptions);

        $layer = $map->GetLayers()->GetItem('Parcels');

        $selection = new MgSelection($map);

        $selection->AddFeatures($layer, $featureReader, 0);

        $selectionXml = $selection->ToXml();

        echo 'Selecting parcels owned by Schmitt in District&nbsp;1';

      }

      catch (MgException $e)

      {

        echo $e->GetMessage();

        echo $e->GetDetails();

      }

      ?>

    </body>

    <script language="javascript">

      // Emit this function and assocate it with the onLoad event

      // for the page so that it gets executed when this page

      // loads in the browser. The function calls the

      // SetSelectionXML method on the Viewer Frame, which updates

      // the current selection on the viewer and the server.

      function OnPageLoad()

      {

        selectionXml = '<?php echo $selectionXml; ?>';

        parent.parent.SetSelectionXML(selectionXml);

      }

    </script>