例: アクティブな選択情報を設定する

次の例では、Web API で選択情報を作成する場合に必要なメソッド部分とビューアに選択情報を返し、ビューアでアクティブな選択情報を設定する場合に必要なメソッド部分を組み合わせて使用しています。「例: 選択したフィーチャを一覧表示する」の説明を拡張した例です。

この例で使用される PHP コードから、選択用 XML が作成されます。その後に、選択情報とともに SetSelectionXML() 関数を呼び出す JavaScript 関数が続きます。JavaScript 関数は、ページのロード時に実行されます。

<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>