| 1 | [[PageOutline]] |
| 2 | |
| 3 | This page is one of the !MapGuide Community CodeSamples. Visit the CodeSamples page to view more! |
| 4 | |
| 5 | == Dynamically adding a layer to the initial map == |
| 6 | The following script shows how to create a temporary !MapDefinition and insert a layer that is visible when the map loads. |
| 7 | It can also be modified to toggle a layers initial visibility. |
| 8 | |
| 9 | == Things to note == |
| 10 | - The code assumes that you have already established a session |
| 11 | - The code can easily be used together with [wiki:CodeSamples/PHP/InitialMapView setting an intial map view] |
| 12 | - $mdResourceId is the full resource id for the !MapDefinition to use |
| 13 | - $rlLayerResourceId is the full resource id for the !LayerDefintion to use |
| 14 | - $layerName is the name of the layer to insert |
| 15 | |
| 16 | == The code == |
| 17 | {{{ |
| 18 | #!php |
| 19 | <?php |
| 20 | |
| 21 | // |
| 22 | // Vital includes. |
| 23 | // |
| 24 | $viewerDir = "mapviewerphp\\"; |
| 25 | include $viewerDir . "constants.php"; |
| 26 | include $viewerDir . "common.php"; |
| 27 | |
| 28 | // Build a string pointing to the new layer in the Session |
| 29 | $rlLayerResourceId = "Session:$sessionId//$layerName.LayerDefinition"; |
| 30 | |
| 31 | // Read the XML of the Library Map Definition |
| 32 | $mdReader = $resourceService->GetResourceContent($mdResourceId); |
| 33 | $mdXml = $mdReader->ToString(); |
| 34 | $mdDomDoc = DOMDocument::loadXML($mdXml); |
| 35 | |
| 36 | // Create the MapLayer XML nodeset in the first position |
| 37 | $targetNode = $mdDomDoc->getElementsByTagName("MapLayer")->item(0); |
| 38 | $newNode = $targetNode->parentNode->insertBefore(new DOMElement("MapLayer"), |
| 39 | $targetNode); |
| 40 | $newNode->appendChild($mdDomDoc->createElement("Name", $layerName)); |
| 41 | $newNode->appendChild($mdDomDoc->createElement("ResourceId", |
| 42 | $rlLayerResourceId)); |
| 43 | $newNode->appendChild($mdDomDoc->createElement("Selectable", "false")); |
| 44 | $newNode->appendChild($mdDomDoc->createElement("ShowInLegend", "false")); |
| 45 | $newNode->appendChild($mdDomDoc->createElement("LegendLabel")); |
| 46 | $newNode->appendChild($mdDomDoc->createElement("ExpandInLegend", "false")); |
| 47 | $newNode->appendChild($mdDomDoc->createElement("Visible", "true")); |
| 48 | $newNode->appendChild($mdDomDoc->createElement("Group")); |
| 49 | |
| 50 | // Write the XML out to form the Session Map Definition |
| 51 | $mdUpdatedXml = $mdDomDoc->saveXML(); |
| 52 | ?> |
| 53 | }}} |
| 54 | |
| 55 | |
| 56 | == See also == |
| 57 | [wiki:CodeSamples/PHP/InitialMapView Setting an intial map view] |
| 58 | |
| 59 | |