[[PageOutline]] 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 [wiki:CodeSamples/PHP/InitialMapView 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 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 == [wiki:CodeSamples/PHP/InitialMapView Setting an intial map view]