This page is one of the MapGuide Community CodeSamples. Visit the CodeSamples page to view more!
Dynamically adding a layer to the initial map
The following script shows how to create a temporary MapDefinition and insert a layer that is visible when the map loads. It can also be modified to toggle a layer's initial visibility.
Things to note
- The code assumes that you have already established a session
- The code can easily be used together with setting an intial map view
- $mdResourceId is the full resource id for the MapDefinition to use
- $rlLayerResourceId is the full resource id for the LayerDefintion to use
- $layerName is the name of the layer to insert
The code
<?php // // Vital includes. // $viewerDir = "mapviewerphp\\"; include $viewerDir . "constants.php"; include $viewerDir . "common.php"; // Build a string pointing to the new layer in the Session $rlLayerResourceId = "Session:$sessionId//$layerName.LayerDefinition"; // Read the XML of the Library Map Definition $mdReader = $resourceService->GetResourceContent($mdResourceId); $mdXml = $mdReader->ToString(); $mdDomDoc = DOMDocument::loadXML($mdXml); // Create the MapLayer XML nodeset in the first position $targetNode = $mdDomDoc->getElementsByTagName("MapLayer")->item(0); $newNode = $targetNode->parentNode->insertBefore(new DOMElement("MapLayer"), $targetNode); $newNode->appendChild($mdDomDoc->createElement("Name", $layerName)); $newNode->appendChild($mdDomDoc->createElement("ResourceId", $rlLayerResourceId)); $newNode->appendChild($mdDomDoc->createElement("Selectable", "false")); $newNode->appendChild($mdDomDoc->createElement("ShowInLegend", "false")); $newNode->appendChild($mdDomDoc->createElement("LegendLabel")); $newNode->appendChild($mdDomDoc->createElement("ExpandInLegend", "false")); $newNode->appendChild($mdDomDoc->createElement("Visible", "true")); $newNode->appendChild($mdDomDoc->createElement("Group")); // Write the XML out to form the Session Map Definition $mdUpdatedXml = $mdDomDoc->saveXML(); ?>
See also
Last modified
16 years ago
Last modified on 09/05/08 02:11:12
Note:
See TracWiki
for help on using the wiki.