Example Code

The following sample illustrates basic page structure. It is designed to be called as a task from a Viewer. It connects to a MapGuide server and displays the map name and spatial reference system for the map currently being displayed in the Viewer.

TipThis sample is very similar to the Hello Map sample in the Developer’s Guide samples.

    <html>

       <head><title>Hello, map</title></head>

       <body>

          <p>

          <?php

          // Define some common locations

          $installDir =

            'C:\Program Files\MapGuideOpenSource\\';

          $extensionsDir = $installDir . 'WebServerExtensions\www\\';

          $viewerDir = $extensionsDir . 'mapviewerphp\\';

          // constants.php is required to set some
    enumerations

          // for PHP. The same step is not required for
    .NET

          // or Java applications.

          include $viewerDir . 'constants.php';

          try

          {

             // Get the session information passed from the
    viewer.

             $args = ($_SERVER['REQUEST_METHOD'] == "POST")

               ? $_POST : $_GET;

             $mgSessionId = $args['SESSION']

             $mgMapName = $args['MAPNAME']

             // Basic initialization needs to be done every
    time.

             MgInitializeWebTier("$extensionsDir\webconfig.ini");

             // Get the user information using the session
    id,

             // and set up a connection to the site server.

             $userInfo = new MgUserInformation($mgSessionId);

             $siteConnection = new MgSiteConnection();

             $siteConnection->Open($userInfo);

             // Get an instance of the required service(s).

             $resourceService = $siteConnection->

             CreateService(MgServiceType::ResourceService);

             // Display the spatial reference system used
    for the map.

             $map = new MgMap();

             $map->Open($resourceService, $mgMapName);

             $srs = $map->GetMapSRS();

             echo 'Map <strong>' . $map->GetName() .

                '</strong> uses this reference system: <br />' . $srs;

          }

          catch (MgException $e)

          {

             echo "ERROR: " . $e->GetMessage() . "<br />";

             echo $e->GetStackTrace() . "<br />";

          }

          ?>

          </p>

       </body>

    </html>