Version 2 (modified by 16 years ago) ( diff ) | ,
---|
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.
http://localhost/mapguide/set_intial_view.php?x=507700&y=186000&scale=400000
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.
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
<?php // // 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>"; exit; } $x = $_REQUEST["x"]; $y = $_REQUEST["y"]; $scale = $_REQUEST["scale"]; // // Usual initialisation step. // InitializeWebTier(); // // 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"); exit; ?>
See also
To reposition a MapGuide map at a given x, y and scale using JavaScript, take a look at the ZoomToView() API function.