[mapguide-users] How can I programatically refresh the whole viewer (with map and legend)?

n.adel n.adel at kemet.com.eg
Thu Jun 2 08:28:22 EDT 2011


Hi,
I've just created a function that adds a new layer with a theme to the Site
Repository and I've successfully added the new layer to the mapdefinition
xml file and I've added it to the Legend.

Then I call to refresh the map and legend but the new layer doesn't appear
on map nor on the legend
When I open the Mapguide studio, I find it added to the Library and when I
refresh the weblayout, it appears successfully. But I just can't get it to
refresh on the browser.
The only thing that works, is to manually refresh the browser and then it
successfully appears.
But using the code, doesn't work.
Here's the function in C#:
 public void EditTheme(string sessionId, MgResourceService resService,
MgFeatureService featService, MgSiteConnection siteconn, string sessionid)
    {
       
       LayerDefinitionFactory factory=new LayerDefinitionFactory();

              string areaRule1 = factory.CreateAreaRule("Zone: AGR",
"RTYPE='AGR'", "FFB93B8F");
       string areaRule2 = factory.CreateAreaRule("Zone: EXM", "RTYPE='EXM'",
"FFF9B7FF");
       string areaRule3 = factory.CreateAreaRule("Zone: MER", "RTYPE='MER'",
"FFE6A9EC");
       string areaRule4 = factory.CreateAreaRule("Zone: MFG", "RTYPE='MFG'",
"FFC38EC7");
       string areaRule5 = factory.CreateAreaRule("Zone: RES", "RTYPE='RES'",
"FFD2B9D3");
       string areaRule6 = factory.CreateAreaRule("Zone: S&W", "RTYPE='S&W'",
"FFC6AEC7");
       string areaRule7 = factory.CreateAreaRule("Zone: WTC", "RTYPE='WTC'",
"FFEBDDE2");
       string areaRule8 = factory.CreateAreaRule("Zone: other", "(default)",
"FFC8BBBE");

       string areaTypeStyle = factory.CreateAreaTypeStyle(
areaRule1+areaRule2+areaRule3+areaRule4+areaRule5+ areaRule6
+areaRule7+areaRule8);
         

       string minScale = "0";
       string maxScale = "10000";
       string areaScaleRange = factory.CreateScaleRange(minScale, maxScale,
areaTypeStyle);
          
       string featureClass =
"Library://Exercise/Data/Parcels.FeatureSource";
       string featureName = "SHP_Schema:Parcels";
       string geometry = "SHPGEOM";
       string layerDefinition1 = factory.CreateLayerDefinition(featureClass,
featureName, geometry, areaScaleRange);

       string layerDefinition= layerDefinition1.Replace("&", "&");

       System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
       Byte[] bytes = encoding.GetBytes(layerDefinition);
       
        MgByteSource byteSource = new MgByteSource(bytes,
layerDefinition.Length);
        byteSource.SetMimeType("text/xml");
        MgResourceIdentifier resId = new
MgResourceIdentifier("Library://Exercise/Layers/Parcels2.LayerDefinition");
        resService.SetResource(resId, byteSource.GetReader(), null);


        MgMap map = new MgMap();
        MgResourceIdentifier resId2 = new
MgResourceIdentifier("Library://Exercise/Maps/Sheboygan.MapDefinition");
        String mapName = resId2.GetName();
        map.Create(resService, resId2, "Sheboygan"); 
       
        MgLayer
newLayer=add_layer_resource_to_map(resId,resService,"Parcels2","Parcels2",
map);
        add_layer_to_group(newLayer, "Municipal", "Municipal", map);

   
        MgByteReader mapbytereader=resService.GetResourceContent(resId2);

// Turn the byte reader to string.
        string strMapXml=mapbytereader.ToString();

// Find the spot at the first " <MapLayer>" 
        int insertPoint=strMapXml.IndexOf("<MapLayer>");

// Insert the layer XML into the map XML.
        string
newLayerXML="<MapLayer><Name>Parcels2</Name><ResourceId>Library://Exercise/Layers/Parcels2.LayerDefinition</ResourceId><Selectable>false</Selectable><ShowInLegend>true</ShowInLegend><LegendLabel>Parcels2</LegendLabel><ExpandInLegend>true</ExpandInLegend><Visible>true</Visible><Group>Municipal</Group></MapLayer>";
        string content=  strMapXml.Insert(insertPoint,newLayerXML);
     


        MgByteSource byteSource2 = new
MgByteSource(Encoding.UTF8.GetBytes(content), content.Length);
        MgByteReader mapcontentByteReader = byteSource2.GetReader();

                
             byteSource2.SetMimeType("text/xml");


     
        MgByteReader mapHeader= resService.GetResourceHeader(resId2);
        
        resService.SetResource(resId2,mapcontentByteReader, mapHeader);

}

This function is called from AddTheme.aspx page and this is the html
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
     
</head>
<body onload="RefreshMap()">
    <form id="form1" runat="server">
    <div>
      
    </div>
    </form>
</body>
</html>

And Here's the cs file for the Addtheme.aspx
 protected void Page_Load(object sender, EventArgs e)
    {
        MgUserInformation userInfo = new MgUserInformation("Administrator",
"pass");
        MgSiteConnection siteConn = new MgSiteConnection();

        siteConn.Open(userInfo);

        MgSite site = new MgSite();
        site.Open(userInfo);


        String sessionId = site.CreateSession();
        MgResourceService resService =
(MgResourceService)siteConn.CreateService(MgServiceType.ResourceService);
        MgFeatureService featService =
(MgFeatureService)siteConn.CreateService(MgServiceType.FeatureService);




              UtilityClass utility = new UtilityClass();
       
               utility.EditTheme(sessionId, resService,featService,
siteConn, sessionId );

        Response.Write("New Parcels2 is created.");
        Response.Write("<br><br>");
        Response.Write(" Refresh your browser to see the updates.");
        
   
//-------------------------------------------------------------------------
       
    }

I'm using the Admisnistrator account so I can save the new layer definition
in the site repository.

All I need is to refresh the ajax viewer so I can the changes I've made.

--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/How-can-I-programatically-refresh-the-whole-viewer-with-map-and-legend-tp6430947p6430947.html
Sent from the MapGuide Users mailing list archive at Nabble.com.


More information about the mapguide-users mailing list