By Daniel Du
As a first step, we need to find the layer and toggle the layer visibility by changing the MgLayer::Visibility property. And next, we need to save the map to apply the change, followed by a call to viewer API to refresh the map. If you are using fusion viewer or flexible web layout, you will want to refer to this post as well.
Here is a code snippet to demonstrate it.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using OSGeo.MapGuide;
using System.Collections.Specialized;
public partial class INTERACTING_WITH_LAYERS_Toggle_Layer_visible
: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
NameValueCollection requestParams
= Request.HttpMethod == "GET" ?
Request.QueryString : Request.Form;
String mgSessionId = requestParams["SESSION"];
String mgLocale = requestParams["LOCALE"];
String mgMapName = requestParams["MAPNAME"];
// Initialize the web-tier.
String realPath = Request.ServerVariables["APPL_PHYSICAL_PATH"];
String configPath = realPath + "..\\webconfig.ini";
MapGuideApi.MgInitializeWebTier(configPath);
// Connect to the site.
MgUserInformation userInfo = new MgUserInformation(mgSessionId);
MgSiteConnection siteConnection = new MgSiteConnection();
siteConnection.Open(userInfo);
// Get an instance of the required service(s).
MgResourceService resourceService
= siteConnection.CreateService(
MgServiceType.ResourceService
) as MgResourceService;
// Get the current map.
MgMap map = new MgMap();
map.Open(resourceService, mgMapName);
// Get map layers.
MgLayerCollection mgLayers = map.GetLayers();
MgLayer roadsLayer = mgLayers.GetItem("Parcels") as MgLayer;
//toggle the visibility.
roadsLayer.Visible = !roadsLayer.Visible;
// Save the updated map to apply the change
map.Save(resourceService);
Response.Write("Parcels Layers visible togged!");
//refresh the map using the viewer API
Response.Write("<script>parent.parent.Refresh();</script>");
}
}
Another way to refresh map is:
<script type="text/javascript">
function RefreshMap() {
parent.parent.Refresh();
}
</script>
</head>
<body onload="javascript:RefreshMap()">
</body>