.. index: single: samples MapGuide Cookbook ================= This section is a select list of MapGuide code samples. See `Trac `_ for more samples. .. _event-hooking-example: JavaScript: Hooking to events in the AJAX viewer ------------------------------------------------ This sample will show how to do the following: * Checking when the map has been loaded. * Handling viewer selection. It is assumed you know what javascript anonymous functions are and how they work. Checking when the map has been loaded ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Before we can hook on to events in the ajax viewer, we need to check that the DOM for the ajax viewer has been loaded. This sample shows how to do it. Assume your viewer page looks something as follows: .. highlight:: html .. code-block:: html Viewer Sample Application Insert the following script block in the head of the page: .. highlight:: html .. code-block:: html Handling viewer selection ^^^^^^^^^^^^^^^^^^^^^^^^^ This sample will show how to handle the selection in the ajax viewer. This assumes the html is the same as the previous example. The code "overrides" the `OnSelectionChanged` function of the map frame (this is called by the map frame when selection changes) and replaces it with our own. Insert the following script block in the head of the page: .. highlight:: html .. code-block:: html when you make a selection on the map, the code will display a dialog showing how many features were selected. Note: Our custom handler executes when a selection has changed. So clearing the selection, for example will also call the handler even though we haven't actually made a selection. JavaScript: Invoke Viewer Command on startup -------------------------------------------- This html page can automatically invoke a named viewer command on viewer startup based on the value of a Cmd query string property. This uses the InitialTask property of the Web Layout and requires that the Task Pane is set to be visible For example setting this initial task pane url in the Web Layout: http://path/to/autostart.html?Cmd=Measure Will launch the Measure command on viewer startup. As you can see from the code below, we need to employ initialization checks just like in the example (:ref:`event-hooking-example`), to make sure we can start using the Viewer API when the frames have been fully initialized. autostart.html .. highlight:: html .. code-block:: html Auto-start command in task pane PHP: 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 , and 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 * The $wl variable should be changed to reflect the relevant WebLayout identifier The code ^^^^^^^^ .. highlight:: php .. code-block:: php One or more of the required arguments is missing."; 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; ?>