This page is one of the MapGuide Community CodeSamples. Visit the CodeSamples page to view more!

Dynamically setting initial map view and scale

The following script allows us to set the initial x, y and scale attributes of a map using the MapGuide Ajax Viewer. It expects the values to be passed to the script as querystring parameters, e.g.


It works by taking a copy of the WebLayout stored in the Library repository and changing the <CenterX/>, <CenterY/> and <Scale/> elements. The updated XML is written into the Session and is used as the target when the page is ultimately redirected.

This code could easily be converted to C# (or whatever).

Things to note

  • A side-effect of the code is that it facilitates anonymous connections
  • The parameter-checking could be made a little more robust *cough*
  • The $wl variable should be changed to reflect the relevant WebLayout identifier

The code


// Vital includes.
$viewerDir = "mapviewerphp\\";
include $viewerDir . "constants.php";
include $viewerDir . "common.php";

// Check and get the required parameters.
if (!isset($_REQUEST["x"]) || !isset($_REQUEST["y"]) || !isset($_REQUEST["scale"])) {
        echo "<Error>One or more of the required arguments is missing.</Error>";

$x = $_REQUEST["x"];
$y = $_REQUEST["y"];
$scale = $_REQUEST["scale"];

// Usual initialisation step.

// Obtain a new session ID for this anonymous user, use it to set up
// a new site connection and use that to create a resource service.
$site = new MgSite();
$site->Open(new MgUserInformation("Anonymous", ""));

$sessionId = $site->CreateSession();

$siteConnection = new MgSiteConnection();
$siteConnection->Open(new MgUserInformation($sessionId));

$resourceService = $siteConnection->CreateService(MgServiceType::ResourceService);

// Read the web layout into an XML DOM document object.
$wl = "Library://WebPID/WebPID_SDF_Static.WebLayout"; // TODO Constant!
$wlResourceId = new MgResourceIdentifier($wl);
$wlReader = $resourceService->GetResourceContent($wlResourceId);
$wlXml = $wlReader->ToString();
$wlDomDoc = DOMDocument::loadXML($wlXml); 

// Now, update the initial x, y and scale values with the desired values.
$nodeCenterX = $wlDomDoc->getElementsByTagName("CenterX")->item(0);
$nodeCenterX->nodeValue = "$x";

$nodeCenterY = $wlDomDoc->getElementsByTagName("CenterY")->item(0);
$nodeCenterY->nodeValue = "$y";

$nodeScale = $wlDomDoc->getElementsByTagName("Scale")->item(0);
$nodeScale->nodeValue = "$scale";

// Prepare the updated XML to be written out to the session.
$updatedXml = $wlDomDoc->saveXML();
$byteSource = new MgByteSource($updatedXml, strlen($updatedXml));

// Create a web layout in the session to hold the updated version
// from the library.
$sessionMapName = $wlResourceId->GetName();
$sessionWebLayout = "Session:$sessionId//$sessionMapName.WebLayout";
$sessionResourceId = new MgResourceIdentifier($sessionWebLayout);

// Write the updated web layout to the session.
$resourceService->SetResource($sessionResourceId, $byteSource->GetReader(), null);

// Redirect to the Ajax viewer pointing at the map at the desired coordinates.
$redirectTo = "mapguide/mapviewerajax/?SESSION=$sessionId&WEBLAYOUT=$sessionWebLayout";
$host = $_SERVER["HTTP_HOST"];
$url = "http://$host/$redirectTo";

// Redirect!
header("Location: $url");


See also

Last modified 16 years ago Last modified on Sep 5, 2008, 2:10:09 AM
Note: See TracWiki for help on using the wiki.