This is a simple example using <a href="http://ASP.NET">ASP.NET</a> and OpenLayers using the usual Sheboygan example. Sorry but I don't know yet how to load legend. For the selection I have not tried yet, but OpenLayers accepts the xml returned by selection in params, so using selection as in MapGuide Dev''s guide should work [see comment in code] - I didn't tested.<br>
For tooltips you can query the server on mouse move, standard AJAX stuff, but I would do it only in intranets or with big server power, otherwise you would be bombing the server with requests.<br>To have more layers, you can add an overlay [<i>in options
useOverlay=true, useAsyncOverlay=true, the second only for
MapGuide>=2.1</i>] using the same session, just changing show/hide
layers string.<br>
And yes, to come up with the following code was really hard! <br>NOTE: call in <b>Global.asax</b> <span style="font-family: courier new,monospace;">MapGuideApi.MgInitializeWebTier(Webconfig Ini Physical Path);</span><br>
<br><i>Devs guide says this shoul be called on every request, I call it once in <b>Global.asax</b>, and it works. I don't know more and haven't found any info!</i><br><br>
<br><br><span style="font-family: courier new,monospace;"><%@ Page Language="C#" %><br><%@ Import Namespace="OSGeo.MapGuide" %><br><br><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>"><br>
<br><script runat="server"><br> private string _strSessionId = "";<br> private string _mapName = "";<br> string _csvShowLayers = "";<br> string _csvHideLayers = "";<br>
<br> protected string GetMapName()<br> {<br> return _mapName;<br> }<br> protected string GetMapSession()<br> {<br> return _strSessionId;<br> }<br> protected string GetShowlayers()<br> {<br>
return _csvShowLayers;<br> }<br> protected string GetHidelayers()<br> {<br> return _csvHideLayers;<br> }<br><br> private void MgDispose(MgDisposable obj)<br> {<br> if (obj != null) obj.Dispose();<br>
}<br><br> protected void Page_Load(object sender, EventArgs e)<br> {<br> MgUserInformation userInfo = null;<br> MgSiteConnection siteConnection = null;<br> MgSite site = null;<br> MgResourceIdentifier resourceId = null;<br>
MgMappingService mappingService = null;<br><br> MgMap map = null;<br> MgResourceService resourceService = null;<br> MgSelection selection = null;<br><br> MgLayerCollection lColl = null;<br>
MgLayerBase layerBase = null;<br> MgResourceIdentifier sessionIdResourceIdentifier = null;<br><br> try<br> {<br> userInfo = new MgUserInformation("Anonymous", "");<br>
siteConnection = new MgSiteConnection();<br> siteConnection.Open(userInfo);<br><br> site = siteConnection.GetSite();<br> _strSessionId = site.CreateSession();<br><br> //---------------------------------------------------<br>
//Save new mapguide session<br> userInfo.SetMgSessionId(_strSessionId);<br> //---------------------------------------------------<br><br> resourceId = new MgResourceIdentifier("Library://Samples/Sheboygan/Maps/Sheboygan.MapDefinition");<br>
_mapName = resourceId.GetName();<br><br> //------------------------------------------------<br> //Layers objectId<br> mappingService = (MgMappingService)siteConnection.CreateService(MgServiceType.MappingService);<br>
map = new MgMap();<br> resourceService = siteConnection.CreateService(MgServiceType.ResourceService) as MgResourceService;<br> map.Create(resourceService, resourceId, _mapName);<br><br> lColl = map.GetLayers();<br>
int iMax = lColl.Count;<br><br><br> //Let's show only Districts & Hydrography:<br> StringBuilder sbShow = new StringBuilder();<br> StringBuilder sbHide = new StringBuilder();<br>
int iHid = 0, iShow = 0;<br> for (int i = 0; i < iMax; ++i)<br> {<br> if (null != layerBase) layerBase.Dispose();<br> layerBase = lColl[i];<br> string layername = layerBase.GetName();<br>
if (layername == "Districts")<br> {<br> if (iShow > 0) sbShow.Append(",");<br> sbShow.Append(layerBase.GetObjectId());<br> ++iShow;<br>
}<br> else if (layername == "Hydrography")<br> {<br> if (iShow > 0) sbShow.Append(",");<br> sbShow.Append(layerBase.GetObjectId());<br>
++iShow;<br> }<br> else<br> {<br> if (iHid > 0) sbHide.Append(",");<br> sbHide.Append(layerBase.GetObjectId());<br>
++iHid;<br> }<br> }<br> _csvHideLayers = sbHide.ToString();<br> _csvShowLayers = sbShow.ToString();<br> //------------------------------------------------<br>
<br> //------------------------------------------------<br> //Necessary to show maps:<br> selection = new MgSelection(map);<br> selection.Save(resourceService, _mapName);<br><br>
sessionIdResourceIdentifier = new MgResourceIdentifier(<br> String.Concat("Session:", _strSessionId, "//", _mapName, ".", MgResourceType.Map));<br> map.Save(resourceService, sessionIdResourceIdentifier);<br>
//------------------------------------------------<br> }<br> catch<br> {<br> throw;//TODO: Handle exceptions<br> }<br> finally<br> {<br> MgDispose(sessionIdResourceIdentifier);<br>
MgDispose(selection);<br><br> MgDispose(layerBase);<br> MgDispose(lColl);<br><br> MgDispose(resourceService);<br> MgDispose(map);<br><br> MgDispose(resourceId);<br>
MgDispose(mappingService);<br> MgDispose(site);<br> MgDispose(siteConnection);<br> MgDispose(userInfo);<br> }<br> }<br></script><br><br><html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>"><br>
<head runat="server"><br> <title></title><br> <script type="text/javascript" src="<a href="http://www.openlayers.org/api/OpenLayers.js">http://www.openlayers.org/api/OpenLayers.js</a>"></script><br>
<style type="text/css"><br> #map_mg<br> {<br> width: 500px;<br> height:500px;<br> float: left;<br> border:solid 1px #000;<br> }<br> </style><br> <br></head><br>
<body onload="init()"><br> <form id="form1" runat="server"><br> <div><br> <h2>Qick example: TODO: Dispose all map guide objects, use StringBuilder, etc...</h2><br>
<h3>Showing only Districts/Hydrography</h3><br> </div><br> <div id="map_mg"><br> </div><br> </form><br> <br> <script type="text/javascript"><br>
var addrs = "<a href="http://localhost/">http://localhost/</a>"; //TODO: get the address of your server<br> var mg_url = addrs + "mapguide/mapagent/mapagent.fcgi?USERNAME=Anonymous&";<br>
var metersPerUnit = 111319.4908; //TODO: get value returned from mapguide on server side<br> var inPerUnit = OpenLayers.INCHES_PER_UNIT.m * metersPerUnit;<br> OpenLayers.INCHES_PER_UNIT["dd"] = inPerUnit;<br>
OpenLayers.INCHES_PER_UNIT["degrees"] = inPerUnit;<br> OpenLayers.DOTS_PER_INCH = 96;<br> var _map = null;<br><br> function init() {<br> //TODO: get extent from mapguide on server side<br>
// I usually get extent on server side, I don't write here code, because Double's ToString<br> // in my locle [Italian] gives "," insted of ".", and I have an assembly to make conversion.<br>
// You can find howto do it easily on Programmer's guide or on WebApi help<br> var extent = new OpenLayers.Bounds(-87.865114442365922, 43.665065564837931, -87.595394059497067, 43.823852564430069);<br>
var mapOptions = {<br> maxExtent: extent,<br> maxResolution: 'auto'<br> };<br> _map = new OpenLayers.Map('map_mg', mapOptions);<br><br> var options = {<br>
isBaseLayer: true,<br> buffer: 1,<br> useOverlay: false,<br> useAsyncOverlay: false,<br> singleTile: true<br> //, transitionEffect: 'resize'<br>
};<br> var params = {};<br> params.mapName = '<%= GetMapName() %>';<br> params.session = '<%= GetMapSession() %>';<br> params.hideLayers = '<%= GetHidelayers() %>';<br>
params.showLayers = '<%= GetShowlayers() %>';<br> // - Depending on Groups you can use params.showGroups/hideGroups<br> // - Making a selection os server side, you can get that selection and pass here as param.selectionXml<br>
// - I have read somewhere that locale should be passed [and that in some cases is mandatory] too, but it works for me even without<br><br> var mg_layer = new OpenLayers.Layer.MapGuide("MapGuide Sheboygan map", mg_url, params, options);<br>
_map.addLayer(mg_layer);<br><br> _map.addControl(new OpenLayers.Control.MousePosition());<br> _map.zoomToMaxExtent(); <br> }<br> </script><br></body><br>
</html><br>
</span><br><br><br>Pietro Ianniello<br>
<br>