By Daniel Du
In this topic, I would like to show you how to change the visibility of layer in AIMS Fusion Viewer or flexible web layout. The basic steps will be changing the “Visible” property of MgLayer, or calling MgLayer.SetVisible() method, then refresh the map.
We can refresh the map in body.onload() event. To refresh the map in fusion viewer, we can use the APIs provided by “MapGuideViewerAPI.js”. We import this script file by :
<%--reference the fusion viewer API javascript file--%>
<script type="text/javascript" language="javascript"
src="../mapserver2012/fusion/layers/MapGuide/MapGuideViewerApi.js">
</script>
Here is the complete code for refresh, please note that we need to call Fusion.getWidgetById('Map').reloadMap() to refresh both legend and map.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ToggleLayer.aspx.cs" Inherits="ToggleLayer" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<%--reference the fusion viewer API javascript file--%>
<script type="text/javascript" language="javascript"
src="../mapserver2012/fusion/layers/MapGuide/MapGuideViewerApi.js">
</script>
<script type="text/javascript">
// set isFusion to false if you are using Ajax viewer
var isFusion = true;
function RefreshMap() {
if (isFusion) {
//Refresh();
Fusion = window.top.Fusion;
//following code does not work
//var legend = Fusion.getWidgetById("Legend");
//legend.renderer.update();
//reload the Map to refresh legend
Fusion.getWidgetById('Map').reloadMap();
}
else {
// for basic web layout,
// if using basic weblayout, referenceing
// to MapGuideViewerApi.js should be removed
parent.parent.Refresh();
}
}
</script>
</head>
<body onload="javascript:RefreshMap()">
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Now let’s working on the code behind. this part of work is pretty straight forward, I paste the code as below:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OSGeo.MapGuide;
public partial class ToggleLayer : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string sessionId = Request["Session"].ToString();
string mapName = Request["MapName"].ToString();
try
{
Utility utility = new Utility();
utility.InitializeWebTier(Request);
utility.ConnectToServer(sessionId);
MgSiteConnection siteConnection
= utility.GetSiteConnection();
if (siteConnection == null)
{
Response.Write("fail to get site connection, exit");
return;
}
MgResourceService resService =
(MgResourceService)siteConnection
.CreateService(MgServiceType.ResourceService);
MgLayerBase tmpLayer = null;
MgMap map = new MgMap();
map.Open(resService, mapName);
tmpLayer = map.GetLayers().GetItem("Districts");
tmpLayer.SetVisible(!tmpLayer.IsVisible());
tmpLayer.ForceRefresh();
map.Save(resService);
if (tmpLayer.IsVisible())
Response.Write("<p><b> the Districts layer is turned on </b></p>");
else
Response.Write("<p><b> the Districts layer is turned off </b></p>");
}
catch (MgException ex)
{
Response.Write(ex.Message);
Response.Write(ex.GetDetails());
}
}
}
It is quite simple, but when you running this code snippet, you may find it does not work, it is executed without any problem, but the visibility of layer does not change in map and legend. The reason is due to the page cache, so we need to clear the page cache by adding following code at the beginning of Page_Load function:
Response.CacheControl = "no-cache";
The complete code goes as below:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OSGeo.MapGuide;
public partial class ToggleLayer : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// *IMPORTANT*, clear the cache
Response.CacheControl = "no-cache";
string sessionId = Request["Session"].ToString();
string mapName = Request["MapName"].ToString();
try
{
Utility utility = new Utility();
utility.InitializeWebTier(Request);
utility.ConnectToServer(sessionId);
MgSiteConnection siteConnection
= utility.GetSiteConnection();
if (siteConnection == null)
{
Response.Write("fail to get site connection, exit");
return;
}
MgResourceService resService =
(MgResourceService)siteConnection
.CreateService(MgServiceType.ResourceService);
MgLayerBase tmpLayer = null;
MgMap map = new MgMap();
map.Open(resService, mapName);
tmpLayer = map.GetLayers().GetItem("Districts");
tmpLayer.SetVisible(!tmpLayer.IsVisible());
tmpLayer.ForceRefresh();
map.Save(resService);
if (tmpLayer.IsVisible())
Response.Write("<p><b> the Districts layer is turned on </b></p>");
else
Response.Write("<p><b> the Districts layer is turned off </b></p>");
}
catch (MgException ex)
{
Response.Write(ex.Message);
Response.Write(ex.GetDetails());
}
}
}
The implementation of Utility class goes as below:
using System;
using System.Collections.Generic;
using System.Web;
using System.Collections;
using System.Xml;
using System.IO;
using System.Text;
using OSGeo.MapGuide;
/// <summary>
/// Summary description for Utility.
/// Created by Daniel Du, DevTech
/// </summary>
public class Utility
{
MgSiteConnection siteConnection;
public void InitializeWebTier(HttpRequest Request)
{
string realPath =
Request.ServerVariables["APPL_PHYSICAL_PATH"];
String configPath = realPath + "../webconfig.ini";
MapGuideApi.MgInitializeWebTier(configPath);
}
public void ConnectToServer(String sessionID)
{
MgUserInformation userInfo =
new MgUserInformation(sessionID);
siteConnection = new MgSiteConnection();
siteConnection.Open(userInfo);
}
public MgSiteConnection GetSiteConnection()
{
return siteConnection;
}
}
Hope this helps you.