6 | | To do... |
| 6 | 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. |
| 7 | |
| 8 | http://localhost/mapguide/set_intial_view.php?x=507700&y=186000&scale=400000 |
| 9 | |
| 10 | 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. |
| 11 | |
| 12 | |
| 13 | == Things to note == |
| 14 | - A side-effect of the code is that it facilitates anonymous connections |
| 15 | - The parameter-checking could be made a little more robust *cough* |
| 16 | - The ''$wl'' variable should be changed to reflect the relevant ''WebLayout'' identifier |
| 17 | |
| 18 | |
| 19 | == The code == |
| 20 | {{{ |
| 21 | #!php |
| 22 | <?php |
| 23 | |
| 24 | // |
| 25 | // Vital includes. |
| 26 | // |
| 27 | $viewerDir = "mapviewerphp\\"; |
| 28 | include $viewerDir . "constants.php"; |
| 29 | include $viewerDir . "common.php"; |
| 30 | |
| 31 | // |
| 32 | // Check and get the required parameters. |
| 33 | // |
| 34 | if (!isset($_REQUEST["x"]) || !isset($_REQUEST["y"]) || !isset($_REQUEST["scale"])) { |
| 35 | echo "<Error>One or more of the required arguments is missing.</Error>"; |
| 36 | exit; |
| 37 | } |
| 38 | |
| 39 | $x = $_REQUEST["x"]; |
| 40 | $y = $_REQUEST["y"]; |
| 41 | $scale = $_REQUEST["scale"]; |
| 42 | |
| 43 | // |
| 44 | // Usual initialisation step. |
| 45 | // |
| 46 | InitializeWebTier(); |
| 47 | |
| 48 | // |
| 49 | // Obtain a new session ID for this anonymous user, use it to set up |
| 50 | // a new site connection and use that to create a resource service. |
| 51 | // |
| 52 | $site = new MgSite(); |
| 53 | $site->Open(new MgUserInformation("Anonymous", "")); |
| 54 | |
| 55 | $sessionId = $site->CreateSession(); |
| 56 | |
| 57 | $siteConnection = new MgSiteConnection(); |
| 58 | $siteConnection->Open(new MgUserInformation($sessionId)); |
| 59 | |
| 60 | $resourceService = $siteConnection->CreateService(MgServiceType::ResourceService); |
| 61 | |
| 62 | // |
| 63 | // Read the web layout into an XML DOM document object. |
| 64 | // |
| 65 | $wl = "Library://WebPID/WebPID_SDF_Static.WebLayout"; // TODO Constant! |
| 66 | $wlResourceId = new MgResourceIdentifier($wl); |
| 67 | $wlReader = $resourceService->GetResourceContent($wlResourceId); |
| 68 | $wlXml = $wlReader->ToString(); |
| 69 | $wlDomDoc = DOMDocument::loadXML($wlXml); |
| 70 | |
| 71 | // |
| 72 | // Now, update the initial x, y and scale values with the desired values. |
| 73 | // |
| 74 | $nodeCenterX = $wlDomDoc->getElementsByTagName("CenterX")->item(0); |
| 75 | $nodeCenterX->nodeValue = "$x"; |
| 76 | |
| 77 | $nodeCenterY = $wlDomDoc->getElementsByTagName("CenterY")->item(0); |
| 78 | $nodeCenterY->nodeValue = "$y"; |
| 79 | |
| 80 | $nodeScale = $wlDomDoc->getElementsByTagName("Scale")->item(0); |
| 81 | $nodeScale->nodeValue = "$scale"; |
| 82 | |
| 83 | // |
| 84 | // Prepare the updated XML to be written out to the session. |
| 85 | // |
| 86 | $updatedXml = $wlDomDoc->saveXML(); |
| 87 | $byteSource = new MgByteSource($updatedXml, strlen($updatedXml)); |
| 88 | |
| 89 | // |
| 90 | // Create a web layout in the session to hold the updated version |
| 91 | // from the library. |
| 92 | // |
| 93 | $sessionMapName = $wlResourceId->GetName(); |
| 94 | $sessionWebLayout = "Session:$sessionId//$sessionMapName.WebLayout"; |
| 95 | $sessionResourceId = new MgResourceIdentifier($sessionWebLayout); |
| 96 | |
| 97 | // |
| 98 | // Write the updated web layout to the session. |
| 99 | // |
| 100 | $resourceService->SetResource($sessionResourceId, $byteSource->GetReader(), null); |
| 101 | |
| 102 | // |
| 103 | // Redirect to the Ajax viewer pointing at the map at the desired coordinates. |
| 104 | // |
| 105 | $redirectTo = "mapguide/mapviewerajax/?SESSION=$sessionId&WEBLAYOUT=$sessionWebLayout"; |
| 106 | $host = $_SERVER["HTTP_HOST"]; |
| 107 | $url = "http://$host/$redirectTo"; |
| 108 | |
| 109 | // |
| 110 | // Redirect! |
| 111 | // |
| 112 | header("Location: $url"); |
| 113 | exit; |
| 114 | |
| 115 | ?> |
| 116 | }}} |
| 117 | |
| 118 | |
| 119 | == See also == |
| 120 | To reposition a MapGuide map at a given ''x'', ''y'' and ''scale'' using JavaScript, take a look at the ZoomToView() API function. |
| 121 | |